我只想通过一个简单的 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"
没有区别.
最佳答案
Spring MVC 默认转换器似乎无法将 Boolean
转换为 text/plain
。仅当我尝试使用 Accept: application/json
请求时,它才起作用。
我建议将返回类型更改为String
,因为无论如何它都是您想要返回的。
@RequestMapping(value = "/", method = RequestMethod.GET)
@ResponseBody
public String isValid() {
return Boolean.TRUE.toString();
}
这样,没有 Accept
header 的请求就可以正常工作,但如果需要,您也可以添加 Accept: text/plain
。
关于java - 如何使用 @RestController 以纯文本形式返回简单的 boolean 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33939424/