我想提供仅提供真/假布尔响应的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.
答案 0 :(得分:12)
您不必删除@ResponseBody
,您可能刚刚删除了MediaType
:
@RequestMapping(value = "/", method = RequestMethod.GET)
@ResponseBody
public Boolean isValid() {
return true;
}
在这种情况下它会默认为application/json
,所以这也会有效:
@RequestMapping(value = "/", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public Boolean isValid() {
return true;
}
如果您指定MediaType.APPLICATION_XML_VALUE
,则您的响应必须可序列化为XML,true
不能。
另外,如果您只想在回复中使用简单的true
,那么它是不是真的是XML?
如果你特别想要text/plain
,你可以这样做:
@RequestMapping(value = "/", method = RequestMethod.GET, produces = MediaType.TEXT_PLAIN_VALUE)
@ResponseBody
public String isValid() {
return Boolean.TRUE.toString();
}