【问题标题】:Spring Framework RestClientException: Extract String from text/plain responseSpring Framework RestClientException:从文本/纯响应中提取字符串
【发布时间】:2015-05-04 14:24:33
【问题描述】:

我有一个带有此端点的 REST API:

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

但我收到此错误:

"无法提取响应:找不到合适的 HttpMessageConverter 对于响应类型 [java.lang.String] 和内容类型 [text/plain]"

我知道正确的方法是发送 JSON 响应,但我必须使用原始字符串。

我该如何解决?

谢谢

【问题讨论】:

  • 首先你有不同的类型:getRole (Integer codEmp) getRol (Long codEmp)

标签: java spring rest


【解决方案1】:

解决了。我在我的 Rest 模板中添加了一个字符串转换器:

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

【讨论】:

    猜你喜欢
    • 2012-11-22
    • 1970-01-01
    • 1970-01-01
    • 2018-06-11
    • 2020-01-03
    • 2014-08-30
    • 2013-08-15
    • 1970-01-01
    • 2018-12-02
    相关资源
    最近更新 更多