SPRINGMVC return XML format, JSON format data

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.