gpt4 book ai didi

java - RestTemplate 如何解析响应

转载 作者:行者123 更新时间:2023-11-29 06:29:52 26 4
gpt4 key购买 nike

开发spring REST Client时,出现一个问题:

  1. 我有下一个 json:


{
"return": [
{
"admin": false,
"alias": "",
"email": "",
"emailId": {"value": 0},
"groups": [],
"id": {"value": 1},
"locked": false,
"loggedInCount": 0,
"master": true,
"sms": "",
"smsId": {"value": 0},
"type": "POWER",
"username": "NGCP"
},
{
"admin": false,
"alias": "",
"email": "",
"emailId": {"value": 0},
"groups": [{"value": 2}],
"id": {"value": 3},
"locked": false,
"loggedInCount": 0,
"master": false,
"sms": "",
"smsId": {"value": 0},
"type": "POWER",
"username": "POLICY"
}
]
}

保存用户的模型类:

@JsonIgnoreProperties(ignoreUnknown = true)
public class User {

public User(){

}

private boolean admin;

private String alias;

private String email;

private String emailId;

private ArrayList<String> groups;

private String id;

private boolean locked;

private int loggedInCount;

private boolean master;

private String sms;

private String smsId;

private String type;

private String userName;

//getter and setters
}

现在我正在使用“RestTemplate”来获得结果。

RestTemplate restTemplate = new RestTemplate();
ResponseEntity<User[]> response = restTemplate.exchange(URL_GET,HttpMethod.GET,request, User[].class);

并得到错误。我知道那是因为主键是“结果”但是我可以指定 restTemplate 应该从哪里解析这个 JSON 吗?

是否可以在喜欢的文件中注明“emailId”以获得直接值(value)?一些模板?

最佳答案

  1. 从“主要关键是结果”开始:

    一个。如果您只处理其中一种 Web 服务,我会为实际负载创建一个包装器类:

    public class Return{
    // Class property cannot be called "return" because it is Java reserved name.
    @JsonProperty("return")
    private User[] array;
    .... getter and setter
    }

    如果您处理多个 web 服务,其中实际有效负载位于“返回”字段中,我将创建一个通用包装类:

    public class Return<T>{
    // Class property cannot be called "return" because it is Java reserved name.
    @JsonProperty("return")
    private T[] array;
    .... getter and setter
    }

    调用 RestRemplate:

    ResponseEntity<Return<User>> response = restTemplate.exchange(URL_GET, 
    HttpMethod.GET, request, new ParameterizedTypeReference<Return<User>>(){});
    User[] usersArray = response2.getBody().getArray();
  2. 对于名为“值”的 JSON 属性中的属性值,我将创建两个自定义 JsonDeserializer(s):一个用于单个值,一个用于值数组,并使用注释每个属性@JsonDeserialize 适用的地方:

    单值反序列化器:

    public class StringValueDeserializer  extends JsonDeserializer<String>{

    @Override
    public String deserialize(JsonParser parser, DeserializationContext ctxt)
    throws IOException, JsonProcessingException {
    ObjectCodec codec = parser.getCodec();
    TreeNode node = codec.readTree(parser);
    JsonNode value = (JsonNode)node.get("value");

    if (value != null){
    return value.asText();
    }
    return null;
    }
    }

    数组值反序列化器:

    public class StringArrayValueDeserializer  extends JsonDeserializer<List<String>>{

    @Override
    public List<String> deserialize(JsonParser parser, DeserializationContext ctxt)
    throws IOException, JsonProcessingException {

    List<String> ret = new ArrayList<>();

    ObjectCodec codec = parser.getCodec();
    TreeNode node = codec.readTree(parser);

    if (node.isArray()){
    for (JsonNode n : (ArrayNode)node){
    JsonNode value = n.get("value");
    if (value != null){
    ret.add(value.asText());
    }
    }
    }
    return ret;
    }
    }

    这是新的User.class:

    public class User {

    private boolean admin;

    private String alias;

    private String email;

    @JsonDeserialize(using = StringValueDeserializer.class)
    private String emailId;

    @JsonDeserialize(using = StringArrayValueDeserializer.class)
    private ArrayList<String> groups;

    @JsonDeserialize(using = StringValueDeserializer.class)
    private String id;

    private boolean locked;

    private int loggedInCount;

    private boolean master;

    private String sms;

    @JsonDeserialize(using = StringValueDeserializer.class)
    private String smsId;

    private String type;

    private String username;
    .... getter and setter
    }

祝你好运!

关于java - RestTemplate 如何解析响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38345893/

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