Héctor
Héctor

Reputation: 26034

Spring Framework RestClientException: Extract String from text/plain response

I have a REST API with this endpoint:

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

An example of response can be: HOUSEKEEPER.

I consume it this way:

@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();
}

But I get this error:

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

I know the right way is to send a JSON response but I have to do it with raw String.

How can I solve it?

Thanks

Upvotes: 6

Views: 8272

Answers (1)

H&#233;ctor
H&#233;ctor

Reputation: 26034

Solved. I have added an String converter to my Rest Template:

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

Upvotes: 9

Related Questions