Spring MVC handles garbled return values

The problem of garbled characters is often encountered in development. The solution is nothing more than keeping the source code/configuration file/database character set consistent.

The garbled problem that the author encountered recently has not been solved by the above method. When I access it with POSTMAN, the Chinese display is normal, but when I access it in the browser, the Chinese display is garbled.

The root of the problem lies in the http header.

The Content-type field in the Http Response Header is not set, and the client does not know what character set the returned content is, and will display it according to the client's own default character set, causing garbled characters.

In the Spring MVC framework, you can try to set the content-type in the returned http response. code show as below.

response.setContentType("text/plain;charset=UTF-8");
It turns out that this does not change the content-type field in the response (you can check the response headers for verification).

The solution is to set the produces attribute of the RequestMapping annotation

@RequestMapping(value = "/", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
In this way, you can see that the content-type becomes the text/plain;charset=UTF-8 we want in the header of the response.


the above

Guess you like

Origin blog.csdn.net/zibaihe007/article/details/78685087