在Spring MVC中通过config验证格式参数

时间:2010-09-07 19:00:43

标签: java json spring spring-mvc content-type

以下是我的情况:

我的myv服务的mvc-config.xml文件设置为将JSON作为默认媒体类型。我也赞成ContentNegotiatingViewResolver的参数为true。另外,我使用NotAcceptableStatusCode为true,因此不接受的格式将返回406。

我的问题是:当有人传入一个不可接受的格式参数(format = foo)时,在配置中是否有办法触发406状态代码?或者必须用代码完成?

这是配置文件:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

    <bean
        class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
        <property name="mediaTypes">
            <map>
                <entry key="json" value="application/json" />
                <entry key="xml" value="application/xml" />
            </map>
        </property>
        <property name="defaultViews">
            <list>
                <bean class="com.work.stuff.web.view.json.ExtendedMappingJacksonJsonView">
                    <property name="objectMapper">
                        <ref bean="JacksonObjectMapper" />
                    </property>
                </bean>
                <bean class="org.springframework.web.servlet.view.xml.MarshallingView">
                    <property name="marshaller">
                        <ref bean="Jaxb2Marshaller" />
                    </property>
                </bean>
            </list>
        </property>
        <property name="defaultContentType" value="application/json" />
        <property name="favorParameter" value="true" />
        <property name="useNotAcceptableStatusCode" value="true" />
    </bean>

    <bean
        class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="messageConverters">
            <list>
                <bean
                    class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
                    <property name="objectMapper">
                        <ref bean="JacksonObjectMapper" />
                    </property>
                </bean>
                <ref bean="marshallingHttpMessageConverter" />
            </list>
        </property>
    </bean>

    <bean id="marshallingHttpMessageConverter"
        class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
        <property name="marshaller" ref="Jaxb2Marshaller" />
        <property name="unmarshaller" ref="Jaxb2Marshaller" />
    </bean>
    <bean id="JacksonObjectMapper" class="org.codehaus.jackson.map.ObjectMapper" />
    <bean id="JacksonSerializationConfig" class="org.codehaus.jackson.map.SerializationConfig"
        factory-bean="JacksonObjectMapper" factory-method="getSerializationConfig" />
    <bean
        class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
        <property name="targetObject" ref="JacksonSerializationConfig" />
        <property name="targetMethod" value="setSerializationInclusion" />
        <property name="arguments">
            <list>
                <value type="org.codehaus.jackson.map.annotate.JsonSerialize.Inclusion">NON_NULL</value>
            </list>
        </property>
    </bean>
    <bean id="Jaxb2Marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
        <property name="classesToBeBound">
            <list>
                <value>com.work.stuff.Concepts</value>
                <value>com.work.stuff.Concept</value>
                <value>com.work.stuff.Terms</value>
                <value>com.work.stuff.Term</value>
                <value>com.work.stuff.Namespaces</value>
                <value>com.work.stuff.Namespace</value>
                <value>com.work.stuff.Subsets</value>
                <value>com.work.stuff.Subset</value>
                <value>com.work.stuff.Associations</value>
                <value>com.work.stuff.Association</value>
            </list>
        </property>
    </bean>
</beans>

1 个答案:

答案 0 :(得分:3)

ContentNegotiatingViewResolver似乎不支持此类行为。目前,我认为最好的办法是将其子类化并覆盖getMediaTypeFromParameter()方法,以便在不支持媒体类型时抛出异常。

您可以从该方法中抛出任何RuntimeException,如果使用@ResponseStatus注释异常类,则可以控制HTTP响应代码,例如

@ResponseStatus(HttpStatus.NOT_ACCEPTABLE)
public class FormatNotSupportedException extends RuntimeException {
}

从长远来看,我强烈建议您向http://jira.springsource.org提出问题,要求将此类功能添加到ContentNegotiatingViewResolver。他们应该能够将其添加为可选的行为参数。像这样的要求意味着Spring不断变得更好。