[英]How to return a boolean value with rest?
I want to provide a boolean
REST
service that only provides true/false boolean response. 我想提供一个只提供true / false布尔响应的
boolean
REST
服务。
But the following does not work. 但以下不起作用。 Why?
为什么?
@RestController
@RequestMapping("/")
public class RestService {
@RequestMapping(value = "/",
method = RequestMethod.GET,
produces = MediaType.APPLICATION_XML_VALUE)
@ResponseBody
public Boolean isValid() {
return true;
}
}
Result: HTTP 406: The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers.
结果:
HTTP 406: The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers.
You don't have to remove @ResponseBody
, you could have just removed the MediaType
: 您不必删除
@ResponseBody
,您可能刚刚删除了MediaType
:
@RequestMapping(value = "/", method = RequestMethod.GET)
@ResponseBody
public Boolean isValid() {
return true;
}
in which case it would have defaulted to application/json
, so this would work too: 在这种情况下,它将默认为
application/json
,所以这也可以工作:
@RequestMapping(value = "/", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public Boolean isValid() {
return true;
}
if you specify MediaType.APPLICATION_XML_VALUE
, your response really has to be serializable to XML, which true
cannot be. 如果您指定
MediaType.APPLICATION_XML_VALUE
,您的响应实际上必须可序列化为XML,这是true
不可能。
Also, if you just want a plain true
in the response it isn't really XML is it? 另外,如果您只想在响应中使用简单的
true
,那么它不是真的XML吗?
If you specifically want text/plain
, you could do it like this: 如果你特别想要
text/plain
,你可以这样做:
@RequestMapping(value = "/", method = RequestMethod.GET, produces = MediaType.TEXT_PLAIN_VALUE)
@ResponseBody
public String isValid() {
return Boolean.TRUE.toString();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.