Last active
December 28, 2015 09:29
-
-
Save yanhua365/7478994 to your computer and use it in GitHub Desktop.
Spring MVC 3.1 的RequestMapping注解里增加了produces属性,
可以明确指定返回响应的格式为特定的格式,比如XML格式。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@RequestMapping(value = "/only-xml-response", method = RequestMethod.GET, produces = MediaType.APPLICATION_XML_VALUE) | |
@ResponseBody | |
public Object onlyXml(ModelMap modelMap){ | |
//BookDTO 是增加了JAXB注解的一个JavaBean | |
BookDTO b = new BookDTO("Java in Action"); | |
return b; | |
//也可以返回字符串 | |
//return "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><book><name>java in action.</name></book>"; | |
} |
JAXB in JDK6
JAXB is included in JDK6, so, you do not need to include JAXB library manually, as long as object is annotated with JAXB annotation, Spring will convert it into XML format automatically.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
BookDTO的代码是这样的: