【问题标题】:Consuming Rest api not working in Spring Boot使用 Rest api 在 Spring Boot 中不起作用
【发布时间】: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&lt;String&gt; 并获得一个 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&lt;String&gt; 一起使用,但无法正常工作。我无法检索 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
  }

【问题讨论】:

    标签: spring-boot resttemplate


    【解决方案1】:

    我能够解决这个问题,只需将 restTemplate 响应作为字符串返回:

    responseEntity<String> response = restTemplate.exchange(BASE_URL + attrPath + shortname + "&attributes=*", HttpMethod.GET, entity, String.class);
    if(response.getStatusCode() == HttpStatus.OK){
             // parse response body to a JSON object without the first and and last character ie. [ ]
             String body = response.getBody().substring(1, response.getBody().length() - 1);
             JSONObject bodyJson = new JSONObject(body);
        
             // parse value for user href link 
             JSONObject hrefJson = bodyJson.getJSONObject("_links").getJSONObject("self");
             String href = hrefJson.getString("href");
             ...
    

    【讨论】:

      猜你喜欢
      • 2021-05-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-04
      • 2019-04-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多