java如何使用@RestController以纯文本形式返回简单的布尔值?
我只想通过一个简单的servlet返回文本“true”:
@RequestMapping(value = "/", method = RequestMethod.GET)
@ResponseBody
public Boolean isValid() {
return true;
}
结果:406 - The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers.
为什么??我怎样才能返回那个简单的值呢?
如果将返回类型更改为String "true"
,则没有区别
# 1 楼答案
并为请求的
accept
头移除/设置适当的头值阅读有关http状态代码here的更多信息
# 2 楼答案
Spring MVC默认转换器似乎无法将
Boolean
转换为text/plain
。只有当我用Accept: application/json
尝试一个请求时,它才起作用我建议将返回类型改为^{,因为这是您想要返回的
这样,没有
Accept
头的请求就可以正常工作,但如果需要,也可以添加Accept: text/plain