spring_mvc(四)Message Converters

[color=blue]Read a String Read string 'foo'[/color]

@RequestMapping(value="/string", method=RequestMethod.POST)
public @ResponseBody String readString(@RequestBody String string) {
return "Read string '" + string + "'";
}

[color=blue]Write a String Wrote a string[/color]
@RequestMapping(value="/string", method=RequestMethod.GET)
public @ResponseBody String writeString() {
return "Wrote a string";
}

[color=blue]Read a Form Read form map{foo=[bar], fruit=[apple]}[/color]
@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;
}

[color=blue]Write a Form foo=bar&fruit=apple[/color]
@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参考上面的

[color=blue]Read XML Read from XML Foo:bar, Fruit:apple[/color]
@RequestMapping(value="/xml", method=RequestMethod.POST)
public @ResponseBody String readXml(@RequestBody JavaBean bean) {
return "Read from XML: " + bean;
}

[color=blue]Write XML [/color]
<?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");
}


[color=blue]Read JSON Read from JSON Foo:bar, Fruit:apple[/color]
@RequestMapping(value="/json", method=RequestMethod.POST)
public @ResponseBody String readJson(@Valid @RequestBody JavaBean bean) {
return "Read from JSON: " + bean;
}

[color=blue]Write JSON{"foo":"bar", "fruit":"apple"}[/color]
@RequestMapping(value="/json", method=RequestMethod.GET)
public @ResponseBody JavaBean writeJson() {
return new JavaBean("bar", "fruit");
}


[color=blue]Read Atom Read My Atom feed[/color]
@RequestMapping(value="/atom", method=RequestMethod.POST)
public @ResponseBody String readFeed(@RequestBody Feed feed) {
return "Read " + feed.getTitle();
}

[color=blue]Write Atom [/color]
<?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;
}

[color=blue]Read Rss[/color]
@RequestMapping(value="/rss", method=RequestMethod.POST)
public @ResponseBody String readChannel(@RequestBody Channel channel) {
return "Read " + channel.getTitle();
}

[color=blue]Write Rss[/color]
@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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值