Spring MVC méthode de manutention json et la forme params

Je veux la gérer les types de contenu application/x-www-form-urlencoded et de l'application/json dans le seul spring mvc méthode.

J'ai une exigence dans le reste du service à accepter l'entrée sous forme de paramètres ou json. Je peux l'obtenir par écrit les deux méthodes. Le formulaire params ou json, la réponse sera toujours json.

@RequestMapping (method = RequestMethod.POST, produces = {"application/json"},
        consumes = {"application/x-www-form-urlencoded"})
public @ResponseBody Book createBook(Book book)
        throws Exception {
    return book;
}

@RequestMapping (method = RequestMethod.POST, produces = {"application/json"},
        consumes = {"application/json"})
public @ResponseBody Book createBookJSON(@RequestBody Book book)
        throws Exception {
    return book;
} 

Est-il possible de combiner ces deux méthodes dans un et de le faire fonctionner? Toute aide sera très appréciée.

Modifier

J'ai mis en œuvre la même, mon contrôleurs et de configuration sont donnés ci-dessous, mais quand j'envoie json demande-je obtenir les valeurs null comme réponse.

Lorsque j'envoie le formulaire params il fonctionne très bien. Aidez-moi à trouver le problème.

Méthode de contrôleur

 @RequestMapping (method = RequestMethod.POST, produces = {"application/json", "application/xml"}, consumes = {"application/x-www-form-urlencoded", "application/json"})                   
    public @ResponseBody Book createBook(Book book)
            throws Exception {
        return book;
    }

servlet-contexte

<mvc:view-controller path="/" view-name="index"/>
<context:annotation-config />
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="order" value="1" />
<property name="mediaTypes">
<map>
<entry key="xml" value="application/xml" />
<entry key="json" value="application/json" />
</map>
</property>
<property name="defaultViews">
<list>
<!-- JSON View -->
<bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" />
<!-- JAXB XML View -->
<bean class="org.springframework.web.servlet.view.xml.MarshallingView">
<constructor-arg>
<ref bean="jaxb2Marshaller" />
</constructor-arg>
</bean>
</list>
</property>
<property name="ignoreAcceptHeader" value="true" />
</bean>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" >
<property name="order" value="1" />
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" >
<property name="supportedMediaTypes" value="application/json"/>
</bean>
<bean id="marshallingHttpMessageConverter" class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
<property name="marshaller" ref="jaxb2Marshaller" />
<property name="unmarshaller" ref="jaxb2Marshaller" />
<property name="supportedMediaTypes" value="application/xml"/>
</bean>
<bean class = "org.springframework.http.converter.FormHttpMessageConverter">
<property name="supportedMediaTypes" value = "application/x-www-form-urlencoded" />
</bean>
<bean class = "org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes" value = "text/plain;charset=UTF-8" />
</bean>
</list>
</property>
</bean>
<bean id="jaxb2Marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller" >
<property name="classesToBeBound">
<list>
<value>com.lt.domain.Book</value>
</list>
</property>
</bean>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="order" value="2" />
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>

OriginalL'auteur Chakravarthi Bharathi | 2012-12-13