為什么會有這篇文章,因?yàn)樵谔幚磉^程中發(fā)現(xiàn)springmvc不能處理json股淡,這不可能啊身隐,所以寫了下,發(fā)現(xiàn)以前沒有重視@RequestBody唯灵。
- 注: 注意鍵值對和json的區(qū)別:
JSON:
{"username":"zhangsan"}
鍵值對:
Controller
import com.zhb.entity.Account;
import com.zhb.entity.User;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* Created by jarvan4dev on 16/2/25.
*/
@RestController
public class IndexController {
@RequestMapping("/user")
public Account getUser(@RequestBody User user){
return new Account(1,user.getUsername(),"6227001376550927913", 10000.0);
}
}
實(shí)體類
import lombok.Data;
/**
* Created by jarvan4dev on 16/2/25.
*/
@Data //@Data-請參看我的lombok介紹
public class User {
private int id;
private String username;
private String password;
}
import lombok.AllArgsConstructor;
import lombok.Data;
/**
* Created by jarvan4dev on 16/2/25.
*/
@Data
@AllArgsConstructor
public class Account {
private int id;
private String username;
private String cardNo;
private double balance;
}
pom.xml
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>4.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.6</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.6</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.7</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
spring-mvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="com.zhb.controller" />
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<constructor-arg value="UTF-8" />
</bean>
<bean
class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
<property name="features">
<array value-type="com.alibaba.fastjson.serializer.SerializerFeature">
<value>NotWriteDefaultValue</value>
<value>DisableCircularReferenceDetect</value>
</array>
</property>
<property name="supportedMediaTypes">
<list>
<value>application/json;charset=UTF-8</value>
<value>text/html;charset=UTF-8</value>
<value>text/plain;charset=UTF-8</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="UTF-8"/>
<property name="maxUploadSize" value="540000"/>
</bean>
</beans>
web.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
使用curl測試:
curl localhost:8080/sp/user -d '{"id":1,"username":"zhangsan","password":"123"}' -H "Content-type: application/json"
- 注意: 為了讓springmvc處理json贾铝,需要在Controller的參數(shù)中加入 @RequestBody注解,
如:
... getUser(@RequestBody User user){...}