Java 如何从RestTemplate交换方法获取特定对象?
我有一个终点,它返回我这个响应:Java 如何从RestTemplate交换方法获取特定对象?,java,json,spring,rest,web,Java,Json,Spring,Rest,Web,我有一个终点,它返回我这个响应: { "head": { "status": 200, "ok": true, "messages": [], "errors": [], "references": {} }, "body": { "id": "d57a9c7aef9842c2e31a0f49c", "flowId": "f57979d06f9842c3e94f1f197", "creationDate": 147074
{
"head": {
"status": 200,
"ok": true,
"messages": [],
"errors": [],
"references": {}
},
"body": {
"id": "d57a9c7aef9842c2e31a0f49c",
"flowId": "f57979d06f9842c3e94f1f197",
"creationDate": 1470744494732,
"path": "/luafanti/test",
"version": 0,
"elems": {
"xxx": {
"type": "integer",
"value": 200
}
}
}
}
我的问题是,如何创建一个只需json响应的一部分即可填充的模型。例如:
"xxx": {
"type": "integer",
"value": 200
}
"xxx": {
"type": "integer",
"value": 200
}
或者这个:
"elems": {
"xxx": {
"type": "integer",
"value": 200
}
}
使用Jackson,您可以按以下方式定义模型:
@JsonIgnoreProperties(ignoreUnknown=true)
public class MyResponseModel {
private Body body;
public void setBody(Body body) {this.body = body;}
public Body getBody() {return body;}
@JsonIgnoreProperties(ignoreUnknown=true)
public static class Body {
private Elems elems;
// getter and setter for elems
}
@JsonIgnoreProperties(ignoreUnknown=true)
public static class Elems {
private Xxx xxx;
// getter and setter for xxx
}
@JsonIgnoreProperties(ignoreUnknown=true)
public static class Xxx {
private String type;
private String value;
// getter and setter for type and value
}
}
上面的内容相当冗长,特别是如果您只对响应的一小部分感兴趣的话。将响应处理为字符串,然后使用(例如)仅提取您感兴趣的数据可能更为实际。使用Jackson,您可以将模型定义为以下内容:
@JsonIgnoreProperties(ignoreUnknown=true)
public class MyResponseModel {
private Body body;
public void setBody(Body body) {this.body = body;}
public Body getBody() {return body;}
@JsonIgnoreProperties(ignoreUnknown=true)
public static class Body {
private Elems elems;
// getter and setter for elems
}
@JsonIgnoreProperties(ignoreUnknown=true)
public static class Elems {
private Xxx xxx;
// getter and setter for xxx
}
@JsonIgnoreProperties(ignoreUnknown=true)
public static class Xxx {
private String type;
private String value;
// getter and setter for type and value
}
}
上面的内容相当冗长,特别是如果您只对响应的一小部分感兴趣的话。将响应作为字符串处理,然后使用(例如)仅提取您感兴趣的数据可能更实际。您可以使用
simple json.jar
从JSONObject
内部提取该对象
可下载Jar链接-
Maven Jar导入
你真正的目标是什么
{
"head": {
"status": 200,
"ok": true,
"messages": [],
"errors": [],
"references": {}
},
"body": {
"id": "d57a9c7aef9842c2e31a0f49c",
"flowId": "f57979d06f9842c3e94f1f197",
"creationDate": 1470744494732,
"path": "/luafanti/test",
"version": 0,
"elems": {
"xxx": {
"type": "integer",
"value": 200
}
}
}
} // hold this complete object in any string reference Variable.
这里,我假设stringjsonstring
保存了完整的json对象,如上所述
在这里的desiredObject
中,您将获得预期值作为JSONObject
您可以使用
simple json.jar
从JSONObject
可下载Jar链接-
Maven Jar导入
你真正的目标是什么
{
"head": {
"status": 200,
"ok": true,
"messages": [],
"errors": [],
"references": {}
},
"body": {
"id": "d57a9c7aef9842c2e31a0f49c",
"flowId": "f57979d06f9842c3e94f1f197",
"creationDate": 1470744494732,
"path": "/luafanti/test",
"version": 0,
"elems": {
"xxx": {
"type": "integer",
"value": 200
}
}
}
} // hold this complete object in any string reference Variable.
这里,我假设stringjsonstring
保存了完整的json对象,如上所述
在这里的desiredObject
中,您将获得预期值作为JSONObject