在开发Spring REST Client时出现问题:
{
"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
}
现在我正在使用&#34; RestTemplate&#34;得到结果。
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<User[]> response = restTemplate.exchange(URL_GET,HttpMethod.GET,request, User[].class);
并得到错误。我知道这是因为主键是&#34;结果&#34;但是我可以指定restTemplate应该从哪里解析这个JSON吗?
并且可以在文件中说明&#34; emailId&#34;获得直接价值?一些模板?
答案 0 :(得分:5)
截至“主键是结果”:
一个。如果您只处理这种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();
从名为“value”的JSON属性中的属性值开始,我将创建两个自定义JsonDeserializer
(s):一个用于单值,一个用于值数组,并使用{{1注释每个属性它适用的地方:
单值解串器:
@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
答案 1 :(得分:1)
您可以使用注释@JsonRootName指定响应中的根元素。所以试试这个:
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonRootName(value ="result")
public class User {
public User(){
}
private boolean admin;
....
}
答案 2 :(得分:1)
您还可以使用JsonPath库浏览json:
String json = restTemplate.exchange(URL_GET,HttpMethod.GET,request, String.class);
DocumentContext document = JsonPath.parse(content, json);
List<User> users = document.read("$.return.*", new TypeRef<List<User>>() {});