解决PC接口Response接口不规范问题
在对接平台接口或其他第三方接口的时候,有没有遇到过,Response里面的字段名不规范,并不符合你的想象,和你定义的业务model字段名不一致!不知道你有没有,反正我有!
解决这个问题的福音就是:@JsonProperty annotation
Demo as below:
import com.fasterxml.jackson.annotation.JsonProperty;
public class JsonPropertyDemoModel {
/**
* 每条数据的唯一标识
* fromat:W5JTkJ2l6E4
*/
@JsonProperty(value = "abcd")
private String dataId;
public String getDataId() {
return dataId;
}
public void setDataId(String dataId) {
this.dataId = dataId;
}
@Override
public String toString() {
return "JsonPropertyDemoModel{" +
"dataId='" + dataId + '\'' +
'}';
}
}
import com.fasterxml.jackson.databind.ObjectMapper;
import junit.framework.TestCase;
/**
* Created by TonyZeng on 2017/5/23.
*/
public class FpoPushDataTest extends TestCase {
public void testGetPulse() throws Exception {
String json = "{\"abcd\":\"WLVgkMQpG0U\"}";
JsonPropertyDemoModel bean = new ObjectMapper().readValue(json.getBytes(), JsonPropertyDemoModel.class);
System.out.println(bean.toString());
}
}
Result of unit test