org.springframework.web.servlet.view.json

Class MappingJackson2JsonView

    • Constructor Detail

      • MappingJackson2JsonView

        public MappingJackson2JsonView()
        Construct a new MappingJackson2JsonView using default configuration provided by Jackson2ObjectMapperBuilder and setting the content type to application/json.
      • MappingJackson2JsonView

        public MappingJackson2JsonView(ObjectMapper objectMapper)
        Construct a new MappingJackson2JsonView using the provided ObjectMapper and setting the content type to application/json.
        Since:
        4.2.1
    • Method Detail

      • setJsonPrefix

        public void setJsonPrefix(java.lang.String jsonPrefix)
        Specify a custom prefix to use for this view's JSON output. Default is none.
        See Also:
        setPrefixJson(boolean)
      • setPrefixJson

        public void setPrefixJson(boolean prefixJson)
        Indicates whether the JSON output by this view should be prefixed with ")]}', ". Default is false.

        Prefixing the JSON string in this manner is used to help prevent JSON Hijacking. The prefix renders the string syntactically invalid as a script so that it cannot be hijacked. This prefix should be stripped before parsing the string as JSON.

        See Also:
        setJsonPrefix(java.lang.String)
      • setModelKey

        public 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.
        Specified by:
        setModelKey in class AbstractJackson2View
      • setModelKeys

        public void setModelKeys(@Nullable
                                 java.util.Set<java.lang.String> modelKeys)
        Set the attributes in the model that should be rendered by this view. When set, all other model attributes will be ignored.
      • getModelKeys

        @Nullable
        public final java.util.Set<java.lang.String> getModelKeys()
        Return the attributes in the model that should be rendered by this view.
      • setExtractValueFromSingleKeyModel

        public void setExtractValueFromSingleKeyModel(boolean extractValueFromSingleKeyModel)
        Set whether to serialize models containing a single attribute as a map or whether to extract the single value from the model and serialize it directly.

        The effect of setting this flag is similar to using MappingJackson2HttpMessageConverter with an @ResponseBody request-handling method.

        Default is false.

      • writePrefix

        protected void writePrefix(JsonGenerator generator,
                                   java.lang.Object object)
                            throws java.io.IOException
        Description copied from class: AbstractJackson2View
        Write a prefix before the main content.
        Overrides:
        writePrefix in class AbstractJackson2View
        Parameters:
        generator - the generator to use for writing content.
        object - the object to write to the output message.
        Throws:
        java.io.IOException