1

I am writing a Spring Restful webservices application using Spring MVC. I have used content negotiating viewer to respond multiple data formats for eg. If some one requests a URL with .xml extension an XML will be sent in response body similarly if someone requests with an .json extension, an json will be sent in response body.

Now, I want the same process inwards, say if some body wants to post a Json or xml or a simple post from a webpage form using post method to same action, it should be able to handle all these.

This way i will be able to write a Web Service+Web Application in a single Spring MVC+Restful Application.

Thanks in advance for the help :)

2 Answers 2

2

You can use headers attribute of @RequestMapping annotation.

@RequestMapping(value = "/pets", method = RequestMethod.POST, headers="content-type=text/*")

to narrow content-type of requests your method is going to serve.

edit:

If you want to sent different content type in request body, then the only thing you need to do is to define MessageConverter (I assume you already did that) and annotate your method parameter with

@RequestBody 

Spring should deserialize the body of your request using the MessageConverter you defined.

So assuming you have something like:

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    <property name="messageConverters">
        <util:list>
            <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/>
        </util:list>
    </property>
</bean>

<bean id="contentNegotiatingViewResolver" class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
    <property name="mediaTypes">
        <util:map>
            <entry key="json" value="application/json"/>
        </util:map>
    </property>
    <property name="defaultViews">
        <util:list>
            <bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView"/>
        </util:list>
    </property>
</bean>

in your spring context.

Annotating your method like this:

@RequestMapping(method=PUT, value="/user/{user_id}")
public void putUser(@RequestBody User user, @PathVariable int user_id) {
    ...
}

should do the job.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the reply soulcheck, Actually I want content Negotiator the other way and wants to accept all format like json/xml or model attribute into same action when some one posts to the restful url and get that json/xml or model attribute to a model object. Normally what I have been able to search is that there are ways to either process xml or json however i am looking at a dynamic mechanism and want to process different incoming post's and convert it into a model object.
So you want to send different content types to the same method? See my edit.
1

You don't have to do anything. You register converts and they will in turn tell "spring" what Content-types they can handle. XStream registers application/xml and text/xml (perhaps more), jackson registers application/json and so on.

It's all available at http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html#mvc-ann-responsebody

I also don't like the filename-standard, I prefer to leave that to the same converter. In that case it will look at the Accept-header. If you want json, set Accepts: application/json.

3 Comments

Thanks for the reply Andreas, Actually I want content Negotiator the other way and wants to accept all format like json/xml or model attribute into same action when[b] some one posts to the restful url [/b]and get that json/xml or model attribute to a model object. Normally what I have been able to search is that there are ways to either process xml or json however i am looking at a dynamic mechanism and want to process different incoming post's and convert it into a model object.
That sounds pretty much like my answer.
Thanks Andreas. It was helpful for me to get to the solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.