Get the App
SLTechnology News&Howtos  ›  Development  › 

Spring boot solves how to return json to the foreground after returning Chinese garbled code to the foreground and returning json data to the wrong meal.

Shulou Source: shulou.com Published: 2022-06-02 07:28:17 09月30日 Update

This article introduces what to do when spring boot solves the problem of returning json to the foreground after returning Chinese garbled code to the foreground. The content is very detailed. Interested friends can use it for reference. I hope it will be helpful to you.

Spring boot solves the Chinese garbled code when the background returns json to the foreground.

Spring boot solution background return json to the foreground Chinese garbled code, error 500:no convertter for return value of type, Chinese returned to the browser garbled parsed into question mark?

Detailed error information Failed to write HTTP message: org.springframework.http.converter.HttpMessageNotWritableException:No converter found for return value of type: class www.it399.com.controller.file.ResultBean

The background returns json to the foreground to solve the problem of Chinese garbled code. 1. Solve the configuration of Chinese garbled code

@ Configuration

@ EnableWebMvc

@ ComponentScan

Public class MvcConfiguration extends WebMvcConfigurerAdapter {

@ Bean

Public HttpMessageConverter responseBodyConverter () {

StringHttpMessageConverter converter = new StringHttpMessageConverter (Charset.forName ("UTF-8"))

Return converter

}

@ Override

Public void configureMessageConverters (List > converters) {

Super.configureMessageConverters (converters)

/ / resolve Chinese garbled

Converters.add (responseBodyConverter ())

/ / after the above configuration is added to solve the Chinese garbled code, the error 500:no convertter for return value of type will be reported directly when the json data is returned.

Converters.add (messageConverter ())

}

}

In addition to the above configuration, MappingJackson2HttpMessageConverter is also added.

@ Bean

Public MappingJackson2HttpMessageConverter messageConverter () {

MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter ()

Converter.setObjectMapper (getObjectMapper ())

Return converter

}

Note: if responseBodyConverter and MappingJackson2HttpMessageConverter are configured separately to ensure that the former is not overwritten, otherwise springboot will be returned and json will be returned normally, but garbled code will be returned, or the Chinese will not be messed up, but an object or json exception will be returned. 3. Complete code to solve springboot Fan Hu Chinese garbled code and return json 500error

/ * *

* spring boot solves the problem of garbled Chinese characters when the backend returns json to the foreground

* http://www.it399.com/blog/index.jsp

, /

@ Configuration

@ EnableWebMvc

@ ComponentScan

Public class MvcConfiguration extends WebMvcConfigurerAdapter {

@ Bean

Public HttpMessageConverter responseBodyConverter () {

StringHttpMessageConverter converter = new StringHttpMessageConverter (Charset.forName ("UTF-8"))

Return converter

}

@ Bean

Public ObjectMapper getObjectMapper () {

Return new ObjectMapper ()

}

@ Bean

Public MappingJackson2HttpMessageConverter messageConverter () {

MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter ()

Converter.setObjectMapper (getObjectMapper ())

Return converter

}

@ Override

Public void configureMessageConverters (List > converters) {

Super.configureMessageConverters (converters)

/ / resolve Chinese garbled

Converters.add (responseBodyConverter ())

/ / after the above configuration is added to solve the Chinese garbled code, the error 500:no convertter for return value of type will be reported directly when the json data is returned.

Converters.add (messageConverter ())

}

}

This is about what to do when spring boot solves the problem of returning json to the foreground after returning the Chinese garbled code to the foreground. I hope the above content can be helpful to everyone and learn more knowledge. If you think the article is good, you can share it for more people to see.

Tags: Chinese garbled foreground background configuration data how to do content more error UTF-8 help good code information interest method object boy buddy Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno MariaDB Apple Shulou Tech Info vpn