Method of spring framework for processing JSON format data in Java Web project

JSON is a common transfer format, which is a format corresponding to key values. And the data size will be relatively small to facilitate transmission. So JSON is often used in development.

Let's first look at the format of JSON:

Each key corresponds to a value, and each key value pair is connected with a comma. And there is no comma after the last key value pair, and the whole needs to be enclosed in braces.

When a normal servlet returns JSON, it will look like the following:

This is relatively troublesome and difficult to package. When the new version of spring returns JSON, it can be returned directly through @ ResponseBody. This is very good. The approximate code is as follows:

The access method is as follows: http://localhost:8080/spring3/action/json/1 It should be noted that when accessing the controller of spring MVC, a / action is added in the middle, because the mapping path of dispatcherservlet will shield the corresponding JSP if / * is directly used, so a prefix must be added to distinguish JSP from controller

If there is Chinese in the above JSON, there will be garbled code, so modify the spring servlet XML configuration file, modify the messageconverters of the annotationmethodhandleradapter, because the default encoding is iso8895-1, and the code is as follows:

After these steps, the processing of JSON is quite convenient.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>