版权声明:未经作者允许,禁止私自转载! https://blog.csdn.net/qq_42726314/article/details/84024870
两种方式:
第一种:使用阿里的fastjson,对应的包。
首先,在pox.xml添加jar包
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.51</version>
</dependency>
其次,在spring-mvc.xml添加如下配置:
<mvc:annotation-driven>
<mvc:message-converters register-defaults="false">
<bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value> text/html; charset=UTF-8</value>
<value> application/json; charset=UTF-8</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
最后,就可以编写controller了
@Controller
public class JsonController {
@ResponseBody
@RequestMapping(value = "/getJson.do")
public User getJson(int id,String name) {
User user = new User(id,name);
return user;
}
}
第二种:导入下面三个jar包
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.7</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.7</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.7</version>
</dependency>
注意:无需任何配置,可以直接编写controller