0
votes

I'm working on simple Spring-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.

2
What is the return type for those methods? I thought you needed to specify a "type" after the @ResponseBody annotation or at least void?nickdos
Oh, sorry missed return types. They are irrelevant, in fact. It seems, Spring matches request by arguments.rsvato

2 Answers

1
votes

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")
0
votes

Just add @RestController annotation for the controller class.