开发者

Handling JSON and form data request with Spring MVC

I'm working on simple 开发者_如何学CSpring-MVC application and I love new Spring REST features. I'd like to use the same method to process regular form and JSON data. It seems to be a little tricky, however. For example, method

public @ResponseBody String process(@RequestBody Bean bean);

will work for JSON request (Content-type: application/json), and

public @ResponseBody String process(Bean bean);

will match request with Content-type: application/x-www-form-urlencoded.

These methods are obviously will have almost the same content, so I'd prefer to avoid such duplication. With Jersey it's possible with @Consumes annotations, but I can't figure out how to do it with Spring.


First, the above declaration won't compile, because you are having duplicate signature.

Btw, @Consumes wouldn't help, I think, because it only designates what content type the method can handle.

In spring you can specify the content-type with

@RequestMapping(headers="Content-Type=application/json")


Just add @RestController annotation for the controller class.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜