2017-08-04 82 views
0

我想使用RestTemplate訪問POST REST服務,但得到錯誤以下estClientException:無法寫請求:找不到合適的HttpMessageConverter請求類型[xxx.query .XBrainQueryRequest]和內容類型[application/json]。RestClientException:無法寫請求:找不到請求類型合適的HttpMessageConverter

XBrainQueryRequest request = new XBrainQueryRequest(); 
// set query ID 
request.setQueryId(XBrainTradequeryId); 
request.setFlags(new String[]{"ALL_FIELDS"}); 
ObjectMapper objectMapper = new ObjectMapper(); 

logger.info("calling XBrainTradeQuery and Input:{}",objectMapper.writeValueAsString(request)); 

HttpHeaders headers = new HttpHeaders(); 

headers.setContentType(MediaType.APPLICATION_JSON); 

try 
     { 
     restTemplate = new RestTemplate(); 

     ResponseEntity<XBrainTradeList> result=null; 
     xBrainTradeList =null; 

     ResponseEntity<XBrainTradeList> result1 = restTemplate.exchange(XBrainTradeQueryURL, HttpMethod.POST, new HttpEntity(request, headers), XBrainTradeList.class); 

和我XBrainQueryRequest類是如下

@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL) 
public class XBrainQueryRequest { 

    private String queryId; 
    private String[] flags; 
    private String[] attributes; 

    /** 
    * @return the queryId 
    */ 

    public String getQueryId() { 
     return queryId; 
    } 

    public XBrainQueryRequest(String queryId, String[] flags, String[] attributes) { 
     super(); 
     this.queryId = queryId; 
     this.flags = flags; 
     this.attributes = attributes; 
    } 

    public XBrainQueryRequest() { 
    } 

    public XBrainQueryRequest(String queryId, String[] flags) { 
     super(); 
     this.queryId = queryId; 
     this.flags = flags; 
    } 

    /** 
    * @param queryId 
    *   the queryId to set 
    */ 
    public void setQueryId(String queryId) { 
     this.queryId = queryId; 
    } 

    public String[] getFlags() { 
     return flags; 
    } 

    public void setFlags(String[] flags) { 
     this.flags = flags; 
    } 

    public String[] getAttributes() { 
     return attributes; 
    } 

    public void setAttributes(String[] attributes) { 
     this.attributes = attributes; 
    } 

} 

有人可以解釋我爲什麼我收到錯誤以及如何解決它。我對這些東西很陌生。

回答

0

已解決。用objectMapper.writeValueAsString(request)替換請求參數。有請求值的JSON格式問題。舊線
ResponseEntity result1 = restTemplate.exchange(XBrainTradeQueryURL,HttpMethod.POST,new HttpEntity(request,headers),XBrainTradeList.class);

新代碼 ResponseEntity其餘= restTemplate.exchange(XBrainTradeQueryURL,HttpMethod.POST,新HttpEntity(objectMapper.writeValueAsString(請求),標頭),String.class);

此外,我採取了字符串格式的響應。

相關問題