【发布时间】:2021-09-23 08:50:33
【问题描述】:
执行后
ResponseEntity<RetrieveAllAttributes> response =
restTemplate.exchange(BASE_URL + attrPath + shortname + "&attributes=*", HttpMethod.GET, entity, RetrieveAllAttributes.class);
我收到org.springframework.web.client.HttpClientErrorException$NotAcceptable: 406 Not Acceptable: [no body]
我在关注this 文章。
这是我的 HttpEntity:
HttpHeaders headers = new HttpHeaders();
headers.set("Cookie", sessionScopedLdapUser.getToken().get("jsession") + ";" + sessionScopedLdapUser.getToken().get("tpa2"));
headers.set("CSRFToken", sessionScopedLdapUser.getToken().get("csrf"));
HttpEntity<RetrieveAllAttributes> entity = new HttpEntity<RetrieveAllAttributes>(new RetrieveAllAttributes(), headers);
当我改用 HttpEntity<String> 并获得一个 String 类型的 ResponseEntity 时,我可以看到响应正文
[
{"_links":
{"formTemplate":{"href":"xx"},"self":{"href":"xxx","title":"xxx"},"erparent":{"href":"xxx"}},
"_attributes":
{"gender":"F","display":"xx","number":"01000040","mail":"xxx ","fullname":"xxx","title":"Frau","source":"H","givenname":"xxx","erroles":["erpersonstatus":"ACTIVE","postaladdress":"xxx","xhortname":"xxx",}
}
]
我也尝试将 JsonObject 和 JsonNodes 与 ResponseEntity<String> 一起使用,但无法正常工作。我无法检索 href 的值:
if(response.getStatusCode() == HttpStatus.OK){
ObjectMapper mapper = new ObjectMapper();
JsonNode rootNode = mapper.readTree(response.getBody());
JsonNode self_href = rootNode.path("self").path("href"); //---> it does not find the node
String hrefAsText = self_href.asText();
有什么建议吗?
这里是我的消费类:
@JsonIgnoreProperties(ignoreUnknown = true)
public class RetrieveAllAttributes {
private Links _links;
private Attributes _attributes;
... getters and setters
}
@JsonIgnoreProperties(ignoreUnknown = true)
public class Attributes {
private String id;
.... getters and setters
}
@JsonIgnoreProperties(ignoreUnknown = true)
public class Links {
private Self self;
.... getters and setters
}
@JsonIgnoreProperties(ignoreUnknown = true)
public class Self {
private String href;
... getters and setters
}
【问题讨论】: