2015-05-04 76 views
1

提取字符串我有一個REST API與此終結:Spring框架RestClientException:從text/plain的響應

@GET 
@Path("rol/{codEmp}") 
@Produces(MediaType.TEXT_PLAIN) 
public String getRole(@PathParam("codEmp") Long codEmp) { 
    return dao.getRole(codEmp); 
} 

響應的一個例子可以是:HOUSEKEEPER

我消費這種方式:

@Override 
public String getRole(Long codEmp) { 
    HashMap<String, Object> urlVariables = new HashMap<String, Object>(); 
    urlVariables.put("codEmp", codEmp); 
    HttpHeaders httpHeaders = new HttpHeaders(); 
    httpHeaders.setAccept(Collections.singletonList(MediaType.parseMediaType("text/plain"))); 
    HttpEntity<Object> requestEntity = new HttpEntity<Object>(httpHeaders); 
    return restTemplate.exchange(rootUrl.concat("/rol/{codEmp}"), HttpMethod.GET, requestEntity, String.class, urlVariables).getBody(); 
} 

但我得到這個錯誤:

"Could not extract response: no suitable HttpMessageConverter found for response type [java.lang.String] and content type [text/plain]"

我知道正確的方法是發送一個JSON響應,但我有一個原始字符串做。

我該如何解決?

謝謝

+0

首先你有不同的類型: getRole(整數codEmp) getRol(龍codEmp) –

+0

編輯。錯字錯誤。 –

回答

3

已解決。我添加了一個字符串轉換到我的休息模板:

restTemplate.getMessageConverters().add(new StringHttpMessageConverter());