当前位置:网站首页>JSON data development
JSON data development
2022-07-26 00:09:00 【Li Linnan】
JSON Data development
Basic concepts
Json It has been used as a general interface parameter type in enterprise development , On the page ( client ) Parsing is very convenient .SpringMVC about json Provides good support , Here you need to modify the relevant configuration , add to json Data support function
@RequestBody
This annotation is used to read Request Requested body Part of the data , Using the system default configuration HttpMessageConverter To analyze , The corresponding data is then bound to the object to be returned , And then HttpMessageConverter The returned object data is bound to controller On the parameters of the method .
@ResponseBody
This annotation is used to Controller The method returns the object , Through appropriate HttpMessageConverter After converting to the specified format , Write to Response Object's body Data area .
The data returned is not html Pages for tags , When it's data in some other format ( Such as json、xml etc. ) Use ( Usually used for ajax request ).
Related configuration
<!-- add to json rely on jar package -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.10.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.10.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.10.0</version>
</dependency>
<!-- mvc Request mapping Processor and Adapter Configuration -->
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.StringHttpMessageConverter"/>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>
</mvc:message-converters>
</mvc:annotation-driven>
Annotations use
@ResponseBody
package com.xxxx.springmvc.controller;
import com.xxxx.springmvc.vo.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.ArrayList;
import java.util.List;
/** * JSON To configure * @ResponseBody * The data returned is JSON Format */
@Controller
public class JsonController {
/** * @ResponseBody The return is JOSN Formatted data , return JavaBean object * The annotation is set on the method body * @return */
@RequestMapping("query01")
@ResponseBody
public User query01(){
User user = new User();
user.setId(1);
user.setUserName("admin");
user.setUserPwd("123456");
return user;
}
/** * @ResponseBody The return is JOSN Formatted data , return JavaBean object * The annotation is set before the return object of the method , After the modifier * @return */
@RequestMapping("query02")
public @ResponseBody User query02(){
User user = new User();
user.setId(1);
user.setUserName("admin");
user.setUserPwd("123456");
return user;
}
/** * @ResponseBody The return is JOSN Formatted data , The return is JOSN Formatted data , Back to the assembly * @return */
@RequestMapping("query03")
@ResponseBody
public List<User> query03(){
User user = new User();
user.setId(1);
user.setUserName("admin");
user.setUserPwd("123456");
User user2 = new User();
user2.setId(2);
user2.setUserName("zhangsan");
user2.setUserPwd("456789");
User user3 = new User();
user3.setId(3);
user3.setUserName("lisi");
user3.setUserPwd("789789");
List<User> userList = new ArrayList<>();
userList.add(user);
userList.add(user2);
userList.add(user3);
return userList;
}
}
@RequestBody
@RequestBody Annotations are often used to handle content-type Not by default application/x-www-form-urlcoded Type of content , for instance :application/json Or is it application/xml etc. . Generally speaking, it is often used to deal with application/json type .
@RequestBody It received a json Format string , It must be a string .
adopt @RequestBody The JSON The string is bound to the corresponding bean On , Of course , You can also bind them to the corresponding string .
/** * Require front desk delivery json Formatted data * @param user * @return */
@RequestMapping("query04")
@ResponseBody
public User query04(@RequestBody User user) {
System.out.println(user);
return user;
}
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>JSON Handle </title>
<%-- introduce Jquery At the heart of JS file --%>
<script type="text/javascript" src="js/jquery-3.4.1.min.js"></script>
</head>
<body>
<input type="button" value="JSON The test data " onclick="test()"/>
<script type="text/javascript">
/*** Request delivery JSON Formatted data * return JSON Formatted data */
function test(){
$.ajax({
// Request mode Get|Post
type: "post",
// Request path
url: "query04",
// Expected data type returned by the server
dataType: "json",
// Set the data type of the server request type to JSON Format
contentType: "application/json;charset=utf-8",
// Parameters passed to the server
data:'{"userName":"admin","userPwd":"123456"}',
// Callback function , Receive the result of the response returned by the server ( The formal parameter in the function is used to receive the data returned by the server )
success:function(data){
console.log(data);
}
});
}
</script>
</body>
</html>
边栏推荐
猜你喜欢
随机推荐
The bull market is not over yet, and there is still 2021-05-18 in the second half
模块二作业
06_ UE4 advanced_ Set up a large map using the terrain tool
拼多多根据ID取商品详情 API 的使用说明
“动物币”凶猛,陷阱还是机遇?2021-05-12
[brother hero July training] day 24: linear tree
@Autowired注解的底层原理
京东按关键字搜索商品 API 的使用说明
34-SparkSQL自定义函数的使用、SparkStreaming的架构及计算流程、DStream转换操作、SparkStreaming对接kafka和offset的处理
Js理解之路:什么是原型链
回溯——17. 电话号码的字母组合
matlab实时作出串口输出数据的图像
京东获取推荐商品列表 API
The items of listview will be displayed completely after expansion
栈与队列——150. 逆波兰表达式求值
Binary tree related knowledge
BGR and RGB convert each other
The GUI interface of yolov3 (simple, image detection)
Leetcode169-多数元素详解
二叉树——700.二叉搜索树中的搜索









