Q:How do @ResponseBody annotations Specify whether to return XML or JSON
A:the produces property of the @RequestMapping specifies
produces = "application/xml" or produces = "Application/json"
return XML
File encoding is utf-8, need the following so write, otherwise garbled
@RequestMapping (value = "/rss", produces = "application/xml;charset=utf-8")
@ResponseBody
Public String Loadrss () throws Exception {
Return converts XML to the corresponding string
}
The page displays the following results: refer to Https://jiashubing.cn/rss
Return JSON
In addition to the obviously specified method above, you can do so without specifying produces
1. Create an object that implements serialization
public class Result implements Serializable {
Private static final long serialversionuid = -349012453592429794l;
private int Status
Get Set
}
2. Returns the serialized object
@RequestMapping (value = "/checksudoku")
@ResponseBody
Public Result Checkmobilegamessudoku (String sudokulist) {
Result.setstatus (1);
return result;
}
Return XML error attempt 1
A string type is returned from the controller, the XML data is converted to a string in the code, and output to the page
Look at the source code will find that the output is all XML files, but will result in Chinese garbled, and the actual page is displayed, the equivalent of this character as HTML text display, and did not show the XML tag
Return XML error attempt 2
Return objects directly, such as:
Original articles, welcome reprint, reproduced please indicate the source!
SPRINGMVC return XML format, JSON format data