org.springframework.http.converter.json

Class AbstractJsonHttpMessageConverter

    • Field Detail

      • DEFAULT_CHARSET

        public static final java.nio.charset.Charset DEFAULT_CHARSET
        The default charset used by the converter.
    • Constructor Detail

      • AbstractJsonHttpMessageConverter

        public AbstractJsonHttpMessageConverter()
    • Method Detail

      • setJsonPrefix

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

        public void setPrefixJson(boolean prefixJson)
        Indicate 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)
      • read

        public final java.lang.Object read(java.lang.reflect.Type type,
                                           @Nullable
                                           java.lang.Class<?> contextClass,
                                           HttpInputMessage inputMessage)
                                    throws java.io.IOException,
                                           HttpMessageNotReadableException
        Description copied from interface: GenericHttpMessageConverter
        Read an object of the given type form the given input message, and returns it.
        Parameters:
        type - the (potentially generic) type of object to return. This type must have previously been passed to the canRead method of this interface, which must have returned true.
        contextClass - a context class for the target type, for example a class in which the target type appears in a method signature (can be null)
        inputMessage - the HTTP input message to read from
        Returns:
        the converted object
        Throws:
        java.io.IOException - in case of I/O errors
        HttpMessageNotReadableException - in case of conversion errors
      • readInternal

        protected abstract java.lang.Object readInternal(java.lang.reflect.Type resolvedType,
                                                         java.io.Reader reader)
                                                  throws java.lang.Exception
        Template method that reads the JSON-bound object from the given Reader.
        Parameters:
        resolvedType - the resolved generic type
        reader - the Reader to use
        Returns:
        the JSON-bound object
        Throws:
        java.lang.Exception - in case of read/parse failures
      • writeInternal

        protected abstract void writeInternal(java.lang.Object o,
                                              @Nullable
                                              java.lang.reflect.Type type,
                                              java.io.Writer writer)
                                       throws java.lang.Exception
        Template method that writes the JSON-bound object to the given Writer.
        Parameters:
        o - the object to write to the output message
        type - the type of object to write (may be null)
        writer - the Writer to use
        Throws:
        java.lang.Exception - in case of write failures