1、什么是JSON
2、@ResponseBody:加了这个注解就不会走视图解析器,会直接返回一个字符串
代码在下面一个问题中
3、 解决json乱码(可以用注解、或配置spring的xml解决)
@RequestMapping(value = “/j1”,produces = “application/json;charset=utf-8”)
因为没有走视图解析器,所以用过滤器解决不了
package com.lan.controller;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.lan.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(value = "/j1",produces = "application/json;charset=utf-8") 解决json乱码,也可以去sping中配置解决
@RequestMapping(value = "/j1")
@ResponseBody//加了这个注解就不会走视图解析器,会直接返回一个字符串
//@RestController//用这个注解表示类下面的所有方法不会走视图解析器,只会返回字符串
public String json1() throws JsonProcessingException {
//jackson, ObjectMapper
ObjectMapper mapper = new ObjectMapper();
//创建一个对象
User user = new User("兰",3,"男");
//将一个对象转换为json格式的,结果为:{"name":"?","age":3,"sex":"?"}
String str = mapper.writeValueAsString(user);
return str;
}
}
springmvc-servlet.xml
<!--JSON乱码问题配置-->
<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"/>
</bean>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
4、@RestController//用这个注解表示类下面的所有方法不会走视图解析器,只会返回字符串
@RestController这个注解放在类上面
5、将对象存入集合转换为JSON格式
网页中打印结果为:[{“name”:“兰1”,“age”:3,“sex”:“男”},{“name”:“兰2”,“age”:3,“sex”:“男”},
{“name”:“兰3”,“age”:3,“sex”:“男”},{“name”:“兰4”,“age”:3,“sex”:“男”}]
@RequestMapping(value = "/j2")
//网页中打印结果为:[{"name":"兰1","age":3,"sex":"男"},{"name":"兰2","age":3,"sex":"男"},
// {"name":"兰3","age":3,"sex":"男"},{"name":"兰4","age":3,"sex":"男"}]
public String json2() throws JsonProcessingException {
//jackson, ObjectMapper
ObjectMapper mapper = new ObjectMapper();
List<User> userList = new ArrayList<User>();
//创建一个对象,然后存入集合中
User user1 = new User("兰1",3,"男");
User user2 = new User("兰2",3,"男");
User user3 = new User("兰3",3,"男");
User user4 = new User("兰4",3,"男");
userList.add(user1);
userList.add(user2);
userList.add(user3);
userList.add(user4);
String str = mapper.writeValueAsString(userList);
return str;
}
6、将时间(Data)转换为json格式,解析后的默认格式为Timestamp(时间戳)
//将时间转换为json格式,解析后的默认格式为Timestamp(时间戳)
@RequestMapping(value = "/j3")
public String json3() throws JsonProcessingException {
//jackson, ObjectMapper
ObjectMapper mapper = new ObjectMapper();
Date date = new Date();
System.out.println(date);
//ObjectMapper,时间解析后的默认格式为Timestamp(时间戳)
String str = mapper.writeValueAsString(date);
return str;
}
idea中的打印结果
7、可以通过自定义时间格式解决解析后为时间戳的问题
@RequestMapping(value = "/j3")
public String json3() throws JsonProcessingException {
//jackson, ObjectMapper
ObjectMapper mapper = new ObjectMapper();
Date date = new Date();
//自定义日期的格式
SimpleDateFormat sdf=new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");
//sdf.format(date)
return mapper.writeValueAsString(sdf.format(date));
}
8、日期会自动转换为时间戳,可以关闭自动转换,再去设置时间转换格式
ObjectMapper:关闭它里面的自动转换为时间戳的方法
mapper.configure(SerializationFeature.WRITE_DATE_KEYS_AS_TIMESTAMPS,false);
自定义日期的格式代码:
SimpleDateFormat sdf=new SimpleDateFormat(“YYYY-MM-dd HH:mm:ss”); mapper.setDateFormat(sdf);
@RequestMapping(value = "/j3")
public String json3() throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
//关闭时间戳
mapper.configure(SerializationFeature.WRITE_DATE_KEYS_AS_TIMESTAMPS,false);
//自定义日期的格式
SimpleDateFormat sdf=new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");
mapper.setDateFormat(sdf);
//ObjectMapper,时间解析后的默认格式为Timestamp(时间戳)
Date date = new Date();
return mapper.writeValueAsString(date);
}
9、自定义时间格式的工具类:使用工具类通过方法重载可以把前面传入集合的也用工具类来得到结果
package com.lan.utils;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import java.text.SimpleDateFormat;
import java.util.Date;
public class JsonUtils {
//方法重载
public static String getJson(Object object){
return getJson(object,"YYYY-MM-dd HH:mm:ss");
}
public static String getJson(Object object,String dateFormat){
ObjectMapper mapper = new ObjectMapper();
//关闭时间戳
mapper.configure(SerializationFeature.WRITE_DATE_KEYS_AS_TIMESTAMPS,false);
//自定义日期的格式
SimpleDateFormat sdf=new SimpleDateFormat(dateFormat);
mapper.setDateFormat(sdf);
try {
return mapper.writeValueAsString(object);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
return null;
}
}
@RequestMapping(value = "/j4")
public String json4() throws JsonProcessingException {
Date date = new Date();
return JsonUtils.getJson(date,"YYYY-MM-dd HH:mm:ss");
}
UserController:全部代码
package com.lan.controller;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.lan.pojo.User;
import com.lan.utils.JsonUtils;
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
@RestController//用这个注解表示类下面的所有方法不会走视图解析器,只会返回字符串
public class UserController {
//@RequestMapping(value = "/j1",produces = "application/json;charset=utf-8") 解决json乱码,也可以去sping中配置解决
//@ResponseBody//加了这个注解就不会走视图解析器,会直接返回一个字符串
//因为没有走视图解析器,所以用过滤器解决不了
@RequestMapping(value = "/j1")
public String json1() throws JsonProcessingException {
//jackson, ObjectMapper
ObjectMapper mapper = new ObjectMapper();
//创建一个对象
User user = new User("兰",3,"男");
//将一个对象转换为json格式的,结果为:{"name":"?","age":3,"sex":"?"}
String str = mapper.writeValueAsString(user);
return str;
}
@RequestMapping(value = "/j2")
//网页中打印结果为:[{"name":"兰1","age":3,"sex":"男"},{"name":"兰2","age":3,"sex":"男"},
// {"name":"兰3","age":3,"sex":"男"},{"name":"兰4","age":3,"sex":"男"}]
public String json2() throws JsonProcessingException {
//jackson, ObjectMapper
ObjectMapper mapper = new ObjectMapper();
List<User> userList = new ArrayList<User>();
//创建一个对象,然后存入集合中
User user1 = new User("兰1",3,"男");
User user2 = new User("兰2",3,"男");
User user3 = new User("兰3",3,"男");
User user4 = new User("兰4",3,"男");
userList.add(user1);
userList.add(user2);
userList.add(user3);
userList.add(user4);
//String str = mapper.writeValueAsString(userList);
//return str;
//使用工具类
return JsonUtils.getJson(userList);
}
//将时间转换为json格式,解析后的默认格式为Timestamp(时间戳)
@RequestMapping(value = "/j3")
public String json3() throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
//关闭时间戳
mapper.configure(SerializationFeature.WRITE_DATE_KEYS_AS_TIMESTAMPS,false);
//自定义日期的格式
SimpleDateFormat sdf=new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");
mapper.setDateFormat(sdf);
//ObjectMapper,时间解析后的默认格式为Timestamp(时间戳)
Date date = new Date();
return mapper.writeValueAsString(date);
}
//将时间转换为json格式,解析后的默认格式为Timestamp(时间戳)
@RequestMapping(value = "/j4")
public String json4() throws JsonProcessingException {
Date date = new Date();
return JsonUtils.getJson(date,"YYYY-MM-dd HH:mm:ss");
}
}