我想提供boolean REST仅提供true / false布尔响应的服务。
boolean
REST
但是以下方法不起作用。为什么?
@RestController @RequestMapping("/") public class RestService { @RequestMapping(value = "/", method = RequestMethod.GET, produces = MediaType.APPLICATION_XML_VALUE) @ResponseBody public Boolean isValid() { return true; } }
结果: 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.
您不必删除@ResponseBody,也可以删除MediaType:
@ResponseBody
MediaType
@RequestMapping(value = "/", method = RequestMethod.GET) @ResponseBody public Boolean isValid() { return true; }
在这种情况下,它将默认设置为application/json,因此也可以使用:
application/json
@RequestMapping(value = "/", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) @ResponseBody public Boolean isValid() { return true; }
如果指定MediaType.APPLICATION_XML_VALUE,则您的响应实际上必须可序列化为XML,而true不能序列化为XML 。
MediaType.APPLICATION_XML_VALUE
true
另外,如果您只想true在响应中输入纯文本,那不是XML吗?
如果您特别想要text/plain,可以这样做:
text/plain
@RequestMapping(value = "/", method = RequestMethod.GET, produces = MediaType.TEXT_PLAIN_VALUE) @ResponseBody public String isValid() { return Boolean.TRUE.toString(); }