1. Respond to JSON data through the SpringMVC framework
In the SpringMVC framework (including the SpringBoot framework), when the method for processing the request is added before @ResponseBody
or before the controller class is used @RestController
, the return value of the method for processing the request will be used as the response data to the client.
When the server responds to the client with data, the SpringMVC framework will use the "Converter" to convert the return value of the method, and the response headers when processing the response. For different return value types, the SpringMVC framework Different converters are also used automatically .
When the (type of return value of the method of processing requests) that the type of response data String
, it will automatically use StringHttpMessageConverter
the converter, the converter string will automatically return to the client as response data, and, also in response to the first set, By default, the Content-Type
property will be set in the response header, and its value is text/html; charset=ISO-8859-1
. Therefore, in the SpringMVC framework ( excluding the SpringBoot framework), the response String
does not support Chinese by default !
When the type of response data is a type that is not recognized by the SpringMVC framework by default , and the current development environment adds a jackson-databind
dependency, the SpringMVC framework will automatically use jackson-databind
the converter in the process, and jackson-databind
the working method of the converter is to organize the response results into JSON format data, and the response header Content-Type
set become application/json; charset=UTF-8
!
In the project, you only need to ensure that the
jackson-databind
dependencies are added , no additional configuration is required, and there is no need to explicitly use a certain class in the framework!If it is a SpringMVC project that uses XML for related configuration, you need to enable the annotation driver in the Spring configuration file, that is, add in the configuration file:
<annotation-driven />
In general, if you need the SpringMVC framework to respond to data in JSON format, you need:
- Use
@RestController
or@ResponseBody
comment; - Add
jackson-databind
dependencies in the project ; - Customize the return value type of the request processing method (as long as it is a custom type, it must be a type that the SpringMVC framework does not recognize by default)
Of course, if an @RestController
annotation has been used in a certain controller class, it will mean "all requests processed in the current controller class will respond to data", and no forwarding or redirection will be performed. If you must perform forwarding Or redirect, you can:
- Do not use
@RestController
annotations, but add@ResponseBody
annotations one by one before each method that needs to respond to data ; - In
@RestController
the case of use , declare the return value type of the method that needs to be forwarded or redirected as aModelAndView
type.