Respond to JSON data through the SpringMVC framework

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 @ResponseBodyor 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 StringHttpMessageConverterthe 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-Typeproperty 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 Stringdoes 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-databinddependency, the SpringMVC framework will automatically use jackson-databindthe converter in the process, and jackson-databindthe working method of the converter is to organize the response results into JSON format data, and the response header Content-Typeset become application/json; charset=UTF-8!

In the project, you only need to ensure that the jackson-databinddependencies 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 @RestControlleror @ResponseBodycomment;
  • Add jackson-databinddependencies 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 @RestControllerannotation 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 @RestControllerannotations, but add @ResponseBodyannotations one by one before each method that needs to respond to data ;
  • In @RestControllerthe case of use , declare the return value type of the method that needs to be forwarded or redirected as a ModelAndViewtype.

Guess you like

Origin blog.csdn.net/qq_44273429/article/details/107414516