Class AbstractJacksonView

All Implemented Interfaces:
Aware, BeanNameAware, ApplicationContextAware, ServletContextAware, View
Direct Known Subclasses:
JacksonJsonView, JacksonXmlView

public abstract class AbstractJacksonView extends AbstractView
Abstract base class for Jackson 3.x based and content type independent AbstractView implementations.

The following special model entries are supported:

  • A JSON view with a com.fasterxml.jackson.annotation.JsonView key and the class name of the JSON view as value.
  • A filter provider with a tools.jackson.databind.ser.FilterProvider key and the filter provider class name as value.
Since:
7.0
Author:
Sebastien Deleuze
  • Field Details

    • JSON_VIEW_HINT

      protected static final String JSON_VIEW_HINT
    • FILTER_PROVIDER_HINT

      protected static final String FILTER_PROVIDER_HINT
    • updateContentLength

      protected boolean updateContentLength
  • Constructor Details

    • AbstractJacksonView

      protected AbstractJacksonView(MapperBuilder<?,?> builder, String contentType)
    • AbstractJacksonView

      protected AbstractJacksonView(ObjectMapper objectMapper, String contentType)
  • Method Details

    • setEncoding

      public void setEncoding(tools.jackson.core.JsonEncoding encoding)
      Set the JsonEncoding for this view.

      Default is UTF-8.

    • getEncoding

      public final tools.jackson.core.JsonEncoding getEncoding()
      Return the JsonEncoding for this view.
    • setDisableCaching

      public void setDisableCaching(boolean disableCaching)
      Disables caching of the generated JSON.

      Default is true, which will prevent the client from caching the generated JSON.

    • setUpdateContentLength

      public void setUpdateContentLength(boolean updateContentLength)
      Whether to update the 'Content-Length' header of the response. When set to true, the response is buffered in order to determine the content length and set the 'Content-Length' header of the response.

      The default setting is false.

    • prepareResponse

      protected void prepareResponse(HttpServletRequest request, HttpServletResponse response)
      Description copied from class: AbstractView
      Prepare the given response for rendering.

      The default implementation applies a workaround for an IE bug when sending download content via HTTPS.

      Overrides:
      prepareResponse in class AbstractView
      Parameters:
      request - current HTTP request
      response - current HTTP response
    • renderMergedOutputModel

      protected void renderMergedOutputModel(Map<String,Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception
      Description copied from class: AbstractView
      Subclasses must implement this method to actually render the view.

      The first step will be preparing the request: In the JSP case, this would mean setting model objects as request attributes. The second step will be the actual rendering of the view, for example including the JSP via a RequestDispatcher.

      Specified by:
      renderMergedOutputModel in class AbstractView
      Parameters:
      model - combined output Map (never null), with dynamic values taking precedence over static attributes
      request - current HTTP request
      response - current HTTP response
      Throws:
      Exception - if rendering failed
    • writeContent

      protected void writeContent(OutputStream stream, Object object, @Nullable Map<String,Object> hints) throws IOException
      Write the actual JSON content to the stream.
      Parameters:
      stream - the output stream to use
      object - the value to be rendered, as returned from filterModel(java.util.Map<java.lang.String, java.lang.Object>, jakarta.servlet.http.HttpServletRequest)
      hints - additional information about how to serialize the data
      Throws:
      IOException - if writing failed
    • setModelKey

      public abstract void setModelKey(String modelKey)
      Set the attribute in the model that should be rendered by this view.

      When set, all other model attributes will be ignored.

    • filterModel

      protected abstract Object filterModel(Map<String,Object> model, HttpServletRequest request)
      Filter out undesired attributes from the given model.

      The return value can be either another Map or a single value object.

      Parameters:
      model - the model, as passed on to renderMergedOutputModel(java.util.Map<java.lang.String, java.lang.Object>, jakarta.servlet.http.HttpServletRequest, jakarta.servlet.http.HttpServletResponse)
      request - current HTTP request
      Returns:
      the value to be rendered
    • writePrefix

      protected void writePrefix(tools.jackson.core.JsonGenerator generator, Object object) throws IOException
      Write a prefix before the main content.
      Parameters:
      generator - the generator to use for writing content
      object - the object to write to the output message
      Throws:
      IOException
    • writeSuffix

      protected void writeSuffix(tools.jackson.core.JsonGenerator generator, Object object) throws IOException
      Write a suffix after the main content.
      Parameters:
      generator - the generator to use for writing content
      object - the object to write to the output message
      Throws:
      IOException