gpt4 book ai didi

java - 如何从 RestTemplate 交换方法中获取特定对象?

转载 作者:行者123 更新时间:2023-12-01 18:03:26 25 4
gpt4 key购买 nike

我有一个端点返回给我这个响应:

{
"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
}

或者这个:

"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
}
}

上面的内容非常冗长,特别是如果您只对响应的一小部分感兴趣的话。将响应作为字符串处理然后使用例如可能更实用。 JsonPath仅提取您感兴趣的数据。

关于java - 如何从 RestTemplate 交换方法中获取特定对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38866411/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com