org.springframework.web.servlet.view.json

Class AbstractJackson2View

    • Field Detail

      • updateContentLength

        protected boolean updateContentLength
    • Constructor Detail

      • AbstractJackson2View

        protected AbstractJackson2View(ObjectMapper objectMapper,
                                       java.lang.String contentType)
    • Method Detail

      • setObjectMapper

        public void setObjectMapper(ObjectMapper objectMapper)
        Set the ObjectMapper for this view. If not set, a default ObjectMapper will be used.

        Setting a custom-configured ObjectMapper is one way to take further control of the JSON serialization process. The other option is to use Jackson's provided annotations on the types to be serialized, in which case a custom-configured ObjectMapper is unnecessary.

      • getObjectMapper

        public final ObjectMapper getObjectMapper()
        Return the ObjectMapper for this view.
      • setEncoding

        public void setEncoding(JsonEncoding encoding)
        Set the JsonEncoding for this view. By default, UTF-8 is used.
      • getEncoding

        public final JsonEncoding getEncoding()
        Return the JsonEncoding for this view.
      • setPrettyPrint

        public void setPrettyPrint(boolean prettyPrint)
        Whether to use the default pretty printer when writing the output. This is a shortcut for setting up an ObjectMapper as follows:
         ObjectMapper mapper = new ObjectMapper();
         mapper.configure(SerializationFeature.INDENT_OUTPUT, true);
         

        The default value is false.

      • 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(java.util.Map<java.lang.String,java.lang.Object> model,
                                               HttpServletRequest request,
                                               HttpServletResponse response)
                                        throws java.lang.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:
        java.lang.Exception - if rendering failed
      • writeContent

        protected void writeContent(java.io.OutputStream stream,
                                    java.lang.Object object)
                             throws java.io.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>)
        Throws:
        java.io.IOException - if writing failed
      • setModelKey

        public abstract void setModelKey(java.lang.String modelKey)
        Set the attribute in the model that should be rendered by this view. When set, all other model attributes will be ignored.
      • writePrefix

        protected void writePrefix(JsonGenerator generator,
                                   java.lang.Object object)
                            throws java.io.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:
        java.io.IOException
      • writeSuffix

        protected void writeSuffix(JsonGenerator generator,
                                   java.lang.Object object)
                            throws java.io.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:
        java.io.IOException