[英]How to return a boolean value with 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.
您不必刪除@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();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.