赞
踩
引入依赖
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.9</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.9</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.9</version>
</dependency>
SpringMVC配置
<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<constructor-arg value="UTF-8"/>
</bean>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper">
<bean class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean">
<property name="failOnEmptyBeans" value="false"></property>
</bean>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script> <script> <!--Json对象--> var user = { "username": "hahah", "password": "123456" }; $.ajax({ url:"/testJson", type: "GET", async: true, data: user,//Json对象 dataType: 'json', success: function (data) { } }); </script>
public class User {
private String username;
private String password;
//get、set方法
}
(1)可省略@RequestParam注解
@Controller
public class TestJson {
@RequestMapping("/testJson")
@ResponseBody
public String testJson(User user,String username,String password){
System.out.println(user.getUsername());//hahah
System.out.println(user.getPassword());//123456
System.out.println(username);//hahah
System.out.println(password);//123456
return "aaaa";
}
}
(2) 加@RequestParam注解
@RequestMapping("/testJson2")
@ResponseBody
public String testJson2(@RequestParam String username,@RequestParam String password){
System.out.println(username);//hahah
System.out.println(password);//123456
return "aaaa";
}
(1)前端传递数据不用转换为Json字符串:Json.stringify(user)
(2)后端接收参数灵活:
①可以是封装对象 (User)
②可以是单个参数(username,password)
③可以封装对象与单个参数混用(User,username或password)
<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script> <script> var user = { "username": "hahah", "password": "123456" }; $.ajax({ url:"/testJson3", type: "POST", async: true, contentType: "application/json;charset=UTF-8", //使用 application/json;charset=UTF-8 data: JSON.stringify(user), //将JSON对象转换为JSON字符串 dataType: 'json', success: function (data) { } }); </script>
@RequestMapping(value = "/testJson3",method = {RequestMethod.POST})
@ResponseBody
public String testJson3(@RequestBody User user){
System.out.println(user.getUsername());//hahah
System.out.println(user.getPassword());//123456
return "aaaa";
}
@RequestMapping(value = "/testJson4",method = {RequestMethod.POST})
@ResponseBody
public String testJson4(@RequestBody Map map){
System.out.println(map.get("username"));//hahah
System.out.println(map.get("password"));//123456
return "aaaa";
}
@RequestMapping(value = "/testJson5",method = {RequestMethod.POST})
@ResponseBody
public String testJson5(@RequestBody String user) throws IOException {
System.out.println(user); // {"username":"hahah","password":"123456"}
ObjectMapper mapper = new ObjectMapper();
User user1 = mapper.readValue(user, User.class);
System.out.println(user1.getUsername());//hahah
System.out.println(user1.getPassword());//123456
return "aaaa";
}
(1)前端需要使用JSON.stringify()将JSON对象转为JSON字符串
(2)后端接收参数比较麻烦,没有第一种简单,也没有第一种灵活。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。