JSON
Jackson的使用
后端给前端传递数据
1.导入依赖
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.13.3</version>
</dependency>
2.解决乱码问题 配置再springmvc配置文件中
<!--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>
传递一个对象
@Controller
//@RestController
public class UserController {
@RequestMapping("/j1")
@ResponseBody //不会走视图解析器,直接返回一个字符串
public String json1() throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
User user = new User(1,"张三","男");
String str = mapper.writeValueAsString(user);
return str;
}
}
@Controller和@ResponseBody配合使用
也可以直接在类上面使用@RestController 效果是一样的 目的是不走视图解析器直接返回一个字符串
传递多个对象
@RequestMapping("/j2")
@ResponseBody //不会走视图解析器,直接返回一个字符串
public String json2() throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
ArrayList<User> userArrayList = new ArrayList<>();
User user1 = new User(1,"张三","男");
User user2 = new User(2,"李四","男");
User user3 = new User(3,"王五","男");
User user4 = new User(4,"老六","男");
userArrayList.add(user1);
userArrayList.add(user2);
userArrayList.add(user3);
userArrayList.add(user4);
String str = mapper.writeValueAsString(userArrayList);
return str;
}
传递时间
@RequestMapping("/j3")
@ResponseBody //不会走视图解析器,直接返回一个字符串
public String json3() throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
//不使用时间戳的方式
mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS,false);
//自定义日期的格式
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM--dd HH:mm:ss");
mapper.setDateFormat(dateFormat);
Date date = new Date();
//ObjectMapper解析后的时间默认格式为:Timestamp 时间戳
return mapper.writeValueAsString(date);
}
经验点:当一个东西经常出现的时候,那么我们就可以把他写成一个工具类
看源码的时候,可以发现大神们在方法重载的时候,实现了代码的复用,很少将重复的代码反复的写,而是直接调用其他方法,就像下面这样,当传递两个参数的时候,可以自定义时间的格式,那么我只需要一个参数的时候,还要再去new一个ObjectMapper和转换字符串吗?直接调用另外一个方法,给他一个默认的时间格式就行了。
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_DATES_AS_TIMESTAMPS,false);
//自定义日期的格式
SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
mapper.setDateFormat(sdf);
try {
return mapper.writeValueAsString(object);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}
}
那么我们上面三个方法就可以优化为:
@Controller
//@RestController
public class UserController {
@RequestMapping("/j1")
@ResponseBody //不会走视图解析器,直接返回一个字符串
public String json1() throws JsonProcessingException {
User user = new User(1,"张三","男");
return JsonUtils.getJson(user);
}
@RequestMapping("/j2")
@ResponseBody //不会走视图解析器,直接返回一个字符串
public String json2() throws JsonProcessingException {
ArrayList<User> userArrayList = new ArrayList<>();
User user1 = new User(1,"张三","男");
User user2 = new User(2,"李四","男");
User user3 = new User(3,"王五","男");
User user4 = new User(4,"老六","男");
userArrayList.add(user1);
userArrayList.add(user2);
userArrayList.add(user3);
userArrayList.add(user4);
return JsonUtils.getJson(userArrayList);
}
@RequestMapping("/j3")
@ResponseBody //不会走视图解析器,直接返回一个字符串
public String json3() throws JsonProcessingException {
Date date = new Date();
//return JsonUtils.getJson(date);
return JsonUtils.getJson(date,"yyyy-MM-dd HH:mm:ss");
}
}
Fastjson的使用
1.导入依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.78</version>
</dependency>
2.使用
@RequestMapping("/j4")
@ResponseBody //不会走视图解析器,直接返回一个字符串
public String json4() throws JsonProcessingException {
ArrayList<User> userArrayList = new ArrayList<>();
User user1 = new User(1,"张三","男");
User user2 = new User(2,"李四","男");
User user3 = new User(3,"王五","男");
User user4 = new User(4,"老六","男");
userArrayList.add(user1);
userArrayList.add(user2);
userArrayList.add(user3);
userArrayList.add(user4);
return JSON.toJSONString(userArrayList);
}