导入依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>SpringMVC</artifactId>
<groupId>com.kuang</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>springmvc-06-JSON</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.10.0</version>
</dependency>
</dependencies>
</project>
controller
package com.qf.controller;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.qf.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class UserController {
@RequestMapping("/j1")
@ResponseBody//不会走视图解析器,将返回值,直接打印在客户端发起请求的页面
public String json1() throws JsonProcessingException {
//jackson, ObjectMapper
ObjectMapper mapper = new ObjectMapper();
User user = new User(3, "基基", 19);
String s = mapper.writeValueAsString(user);
return s;
}
}
文本默认使用“text/plain;charset=ISO-8859-1”作为ContentType,导致中文乱码
@RequestMapping(value = "/j1",produces = "application/json;charset=utf-8")
<!-- JSON乱码问题-->
<mvc:annotation-driven>
<mvc:message-converters>
<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"/>
</bean>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
package com.qf.controller;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.qf.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
//@Controller//会调用视图解析器,用来转发和重定向的,视图解析器默认是转发,加上 redirect:/
@RestController//不会走视图解析器,这类下的所有方法返回的都是响应给请求页面的字符串。
public class UserController {
@RequestMapping(value = "/j1")
// @ResponseBody//不会走视图解析器,将返回值,直接打印在客户端发起请求的页面
public String json1() throws JsonProcessingException {
//jackson, ObjectMapper
ObjectMapper mapper = new ObjectMapper();
User user = new User(3, "基基", 19);
String s = mapper.writeValueAsString(user);
return s;
}
@RequestMapping(value = "/j2")
// @ResponseBody//不会走视图解析器,将返回值,直接打印在客户端发起请求的页面
public String json2() throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
User user1 = new User(1, "基基1", 19);
User user2 = new User(2, "基基2", 19);
User user3 = new User(3, "基基3", 19);
User user4 = new User(4, "基基4", 19);
List<User> users=new ArrayList<>();
users.add(user1);
users.add(user2);
users.add(user3);
users.add(user4);
String s = mapper.writeValueAsString(users);
return s;
}
@RequestMapping(value = "/j3")
// @ResponseBody//不会走视图解析器,将返回值,直接打印在客户端发起请求的页面
public String json3() throws JsonProcessingException {
Date date = new Date();
//ObjectMapper 时间解析过后的默认格式的为时间戳:Timestap;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String format = sdf.format(date);
return new ObjectMapper().writeValueAsString(format);
}
@RequestMapping(value = "/j4")
// @ResponseBody//不会走视图解析器,将返回值,直接打印在客户端发起请求的页面
public String json4() throws JsonProcessingException {
//将数据转化为JSON格式
ObjectMapper mapper = new ObjectMapper();
//不使用时间戳的方式
mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS,false);
//ObjectMapper 时间解析过后的默认格式的为时间戳:Timestap;
//自定义日期格式
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
mapper.setDateFormat(sdf);
Date date = new Date();
String string = mapper.writeValueAsString(date);
return string;
}
}
封装的工具类
package com.qf.utils;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import java.text.SimpleDateFormat;
public class JsonUtils {
public static String getJson(Object o){
return getJson(o,"yyyy-MM-dd HH-mm-ss");
}
public static String getJson(Object o,String dateFormat){
ObjectMapper mapper = new ObjectMapper();
mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS,false);
SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
mapper.setDateFormat(sdf);
try {
return mapper.writeValueAsString(o);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
return null;
}
}