【实习日记】Spring中JSON与类的绑定


1. 设计与前端交互的JSON接口(登陆与注册部分)

参考了这个JSONAPI设计规范

http://jsonapi.org/

2. 用Wireshark抓包,分析本机发出的HTTP请求是否带上了要求的HEADERS

3. spring request得到的response不能直接绑定成对象,需要先用string接收,然后用jackson转换

import com.fasterxml.jackson.databind.ObjectMapper;

ObjectMapper mapper = new ObjectMapper();
try {
	ress = mapper.readValue(reStr, Response.class);
	System.out.println(ress.getCode());
} catch (IOException e) {
	// TODO Auto-generated catch block
	e.printStackTrace();
}

在jackson转换时,需要把映射类的setter和getter函数写好。否则会转换错误

如果json中没有对应的变量值,jackson会把相应的成员变量置为null。

 

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.