This article brings you an introduction to the method of SpringMVC receiving and responding to json data (with code). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
For front-end and back-end data interaction, in addition to submitting through form forms, you can also transmit and receive json format data to the back-end through ajax (this method can realize the separation of request data and pages). This article will summarize several ways to receive and respond to json data in Spring MVC.
Preparation steps:
1. Import the dependencies of json-related frameworks (such as jackson).
2. The controller method of spring mvc is written normally. If you need to respond to json, add the @responsebody annotation.
3. Before accepting the input parameters corresponding to json, add the @RequestBody annotation.
The server receives json data and restores it to a java object, which is called deserialization. On the contrary, converting the java object into json data as a response and sends it back to the client is called serialization.
Note: Because you want to use ajax, you must introduce jQuery, remember!
jackson maven dependency:
com.fasterxml.jackson.core jackson-core 2.7.0 com.fasterxml.jackson.core jackson-databind 2.7.0 com.fasterxml.jackson.core jackson-annotations 2.7.0
Background: When ajax passes many parameters, it is not convenient to use parameter name matching method. If there is a corresponding entity class in the background, you can choose to encapsulate the data in json format on the client and pass it to the background, and the background will use the corresponding entity class to receive it.
Client:
@responseBody annotation is to convert the object returned by the controller method into the specified format through an appropriate converter, and then write it to the body area of the response object, usually with to return JSON data or XML.@RequestBody annotation is commonly used to handle content whose content-type is not the default application/x-www-form-urlcoded encoding. Generally speaking, it is commonly used to deal with application/json types.
Controller:
@Controller public class PassJsonParam { @RequestMapping(value="acceptJsonByEntity",method = RequestMethod.POST) @ResponseBody public Book acceptJsonByEntity(@RequestBody Book book, HttpServletRequest request){ System.out.println("当前http请求方式为:"+request.getMethod()); System.out.println("bookId="+book.getBookId()+", author="+book.getAuthor()); return book; } }
Console output: The current http request method is: POST bookId=1, author=Jack
Client (pop-up window): success :1,Jack
@RestController
annotation.
@RestController = @Controller @ResponseBody
Controller (the above Controller can be replaced with the following one):
@RestController public class PassJsonParam { @RequestMapping(value="acceptJsonByEntity",method = RequestMethod.POST) public Book acceptJsonByEntity(@RequestBody Book book, HttpServletRequest request){ System.out.println("当前http请求方式为:"+request.getMethod()); System.out.println("bookId="+book.getBookId()+", author="+book.getAuthor()); return book; } }
Note:@RestController
is used After annotation, the Controller's method can no longer return jsp pages or HTML, and the configured view parser will not work.
Client:
Controller:
@RequestMapping(value="acceptJsonByMap") @ResponseBody public MapacceptJsonByMap(@RequestBody Map paramsMap, HttpServletRequest request){ System.out.println("当前http请求方式为:"+request.getMethod()); System.out.println(paramsMap); return paramsMap; }
Console output: The current http request method is: POST {bookName=Love, author=Frank}
Client (Pop-up window): bookName =Love; author=Frank
Client:
Note: When passed to the backend, the list should be json format data of [ { key1 : value1} { key2 : value2} ], otherwise a Json parse error may occur.
Controller:
@RequestMapping(value="acceptJsonByList") @ResponseBody public ListacceptJsonByList(@RequestBody List book, HttpServletRequest request){ System.out.println("当前http请求方式为:"+request.getMethod()); System.out.println(book); return book; }
Note: The Book entity class is required here to receive.
Console output: The current http request method is: POST [entity.Book@1138a75c, entity.Book@22d1cbcf]
Client (pop-up window): bookId =123; author=Rose bookId =321; author=Jack
The above is the detailed content of Introduction to the method of SpringMVC receiving and responding to json data (with code). For more information, please follow other related articles on the PHP Chinese website!