spring_mvc(四)Message Converters
程序员文章站
2022-07-14 12:34:02
...
Read a String Read string 'foo'
Write a String Wrote a string
Read a Form Read form map{foo=[bar], fruit=[apple]}
JavaBean:
Write a Form foo=bar&fruit=apple
JavaBean参考上面的
Read XML Read from XML Foo:bar, Fruit:apple
Write XML
java:
Read JSON Read from JSON Foo:bar, Fruit:apple
Write JSON{"foo":"bar", "fruit":"apple"}
Read Atom Read My Atom feed
Write Atom
<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www/w3/org/2005/Atom">
<title>My Atom feed</title>
</feed>
Read Rss
Write Rss
@RequestMapping(value="/string", method=RequestMethod.POST) public @ResponseBody String readString(@RequestBody String string) { return "Read string '" + string + "'"; }
Write a String Wrote a string
@RequestMapping(value="/string", method=RequestMethod.GET) public @ResponseBody String writeString() { return "Wrote a string"; }
Read a Form Read form map{foo=[bar], fruit=[apple]}
@RequestMapping(value="/form", method=RequestMethod.POST) public @ResponseBody String readForm(@ModelAttribute JavaBean bean) { return "Read x-www-form-urlencoded: " + bean; }
JavaBean:
@XmlRootElement public class JavaBean { @NotNull private String foo; @NotNull private String fruit; }
Write a Form foo=bar&fruit=apple
@RequestMapping(value="/form", method=RequestMethod.GET) public @ResponseBody MultiValueMap<String, String> writeForm() { MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>(); map.add("foo", "bar"); map.add("fruit", "apple"); return map; }
JavaBean参考上面的
Read XML Read from XML Foo:bar, Fruit:apple
@RequestMapping(value="/xml", method=RequestMethod.POST) public @ResponseBody String readXml(@RequestBody JavaBean bean) { return "Read from XML: " + bean; }
Write XML
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <javaBean> <foo>bar</foo> <fruit>apple</fruit> </javaBean>
java:
@RequestMapping(value="/xml", method=RequestMethod.GET) public @ResponseBody JavaBean writeXml() { return new JavaBean("bar", "fruit"); }
Read JSON Read from JSON Foo:bar, Fruit:apple
@RequestMapping(value="/json", method=RequestMethod.POST) public @ResponseBody String readJson(@Valid @RequestBody JavaBean bean) { return "Read from JSON: " + bean; }
Write JSON{"foo":"bar", "fruit":"apple"}
@RequestMapping(value="/json", method=RequestMethod.GET) public @ResponseBody JavaBean writeJson() { return new JavaBean("bar", "fruit"); }
Read Atom Read My Atom feed
@RequestMapping(value="/atom", method=RequestMethod.POST) public @ResponseBody String readFeed(@RequestBody Feed feed) { return "Read " + feed.getTitle(); }
Write Atom
<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www/w3/org/2005/Atom">
<title>My Atom feed</title>
</feed>
@RequestMapping(value="/atom", method=RequestMethod.GET) public @ResponseBody Feed writeFeed() { Feed feed = new Feed(); feed.setFeedType("atom_1.0"); feed.setTitle("My Atom feed"); return feed; }
Read Rss
@RequestMapping(value="/rss", method=RequestMethod.POST) public @ResponseBody String readChannel(@RequestBody Channel channel) { return "Read " + channel.getTitle(); }
Write Rss
@RequestMapping(value="/rss", method=RequestMethod.GET) public @ResponseBody Channel writeChannel() { Channel channel = new Channel(); channel.setFeedType("rss_2.0"); channel.setTitle("My RSS feed"); channel.setDescription("Description"); channel.setLink("http://localhost:8080/mvc-showcase/rss"); return channel; }
上一篇: (一)Redis基础类型
下一篇: (二)Redis投票应用