Spring MVC方法处理json和form params

Cha*_*thi 6 java json spring-mvc java-ee

我想在单弹簧mvc方法中处理内容类型application/x-www-form-urlencoded和application/json.

我在休息服务中要求接受输入作为表单参数或json.我可以通过编写两种方法来实现这一点.无论是形式params还是json,响应将永远是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;
} 
Run Code Online (Sandbox Code Playgroud)

是否可以将这两种方法合二为一,使其有效?任何帮助都感激不尽.

编辑

我实现了相同的,我的控制器和配置如下,但是当我发送json请求时,我得到null值作为响应.

当我发送表格参数时它工作正常.帮我找出问题所在.

控制器方法

 @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;
    }
Run Code Online (Sandbox Code Playgroud)

的servlet上下文

<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>
Run Code Online (Sandbox Code Playgroud)

G-M*_*Man 3

@RequestMapping (method = RequestMethod.POST)
public Book createBook(Book book)
        throws Exception {
    return book;
}
Run Code Online (Sandbox Code Playgroud)

Consumers 接受一个字符串数组,其中包含它可以消耗的任何内容,Spring bean 绑定应该处理其余的事情。问题可能是您没有正确设置 bean 绑定以便自动编组和解组 json。恕我直言,使用 @RequestBody 和 @RepsonseBody 不是最好的选择。

确保 jackson 已添加到您的依赖项中

<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>latest</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)

并使用 contentnegotiatingviewresolver

<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
    <property name="mediaTypes">
        <map>
            <entry key="htm" value="text/htm"/>
            <entry key="html" value="text/html"/>
            <entry key="json" value="application/json"/>
        </map>
    </property>
    <property name="viewResolvers">
        <list>
            <bean class="org.springframework.web.servlet.view.BeanNameViewResolver"/>
            <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
                <property name="prefix" value="/WEB-INF/jsp/"/>
                <property name="suffix" value=".jsp"/>
            </bean>
        </list>
    </property>

    <property name="defaultViews">
        <list>
            <bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView"/>

        </list>
    </property>
</bean>
Run Code Online (Sandbox Code Playgroud)

确保客户端应用程序中的接受标头设置为所需的值。您还应该能够删除 requestmethod 注释中的所有生产和消费数据