如何在REST API post请求结束时避免使用后缀?

时间:2016-12-19 09:37:25

标签: json spring rest spring-mvc

我在my servlet-config.xml中添加了以下内容,并且在spring mvc的GET Requests for REST API的末尾添加了.json的后缀,但对于post API,我仍然需要{最后{1}}

.json

请告诉我如何最终避免<property name="defaultContentType" value="application/json" /> 发送POST请求。

以下是我的.json

servlet-config.xml

以下是我的控制器

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<mvc:annotation-driven />
<context:component-scan base-package="com.test">    </context:component-scan>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/jsp/" p:suffix=".jsp"></bean>
<mvc:annotation-driven content-negotiation-manager="contentNegotiationManager" />
<bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
   <property name="favorPathExtension" value="false"/>
   <property name="favorParameter" value="true" />
   <property name="parameterName" value="mediaType" />
   <property name="ignoreAcceptHeader" value="true"/>
   <property name="useJaf" value="false"/>
   <property name="defaultContentType" value="application/json" />
   <property name="mediaTypes">
      <value>
        json=application/json
        xml=application/xml
      </value>
   </property>
</bean>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/jsp/" p:suffix=".jsp">    </bean>
<!--     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">    </bean>-->
<bean class="org.springframework.web.servlet.view.BeanNameViewResolver" p:order="0">    </bean>
    <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
        <property name="order" value="1" />
        <property name="contentNegotiationManager">
            <bean class="org.springframework.web.accept.ContentNegotiationManager">
                <constructor-arg>
                    <bean class="org.springframework.web.accept.PathExtensionContentNegotiationStrategy">
                        <constructor-arg>
                            <map>
                                <entry key="json" value="application/json">    </entry>
                                <entry key="xml" value="application/xml">    </entry>
                            </map>
                        </constructor-arg>
                    </bean>
                </constructor-arg>
            </bean>
        </property>
        <property name="defaultViews">
            <list>
                <!--     <bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView"  />-->
                <bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView"  />
                <bean class="org.springframework.web.servlet.view.xml.MarshallingView" >
                    <constructor-arg>
                        <bean class="org.springframework.oxm.xstream.XStreamMarshaller">
                            <property name="autodetectAnnotations" value="true" />
                        </bean>
                    </constructor-arg>
                </bean>
            </list>
        </property>
    </bean>

</beans>

1 个答案:

答案 0 :(得分:0)

您还可以定义以下配置,这与您的默认内容类型设置

相矛盾
<constructor-arg>
       <bean class="org.springframework.web.accept.PathExtensionContentNegotiationStrategy">
              <constructor-arg>
                     <map>
                          <entry key="json" value="application/json">    </entry>
                          <entry key="xml" value="application/xml">    </entry>
                     </map>
              </constructor-arg>
       </bean>
</constructor-arg>