【问题标题】:How to avoid a suffix at the end of REST API post request?如何避免 REST API 发布请求末尾的后缀?
【发布时间】:2016-12-19 09:37:24
【问题描述】:

我在我的servlet-config.xml 中添加了以下内容,并摆脱了在 spring mvc 中 REST API 的 GET 请求末尾添加 .json 的后缀,但对于 post API,我仍然需要 .json 在结束

<property name="defaultContentType" value="application/json" />

请告诉我如何才能避免 .json 最终发出 POST 请求。

下面是我的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>

下面是我的控制器

@Controller
public class ObjectResource {

    @RequestMapping(value = "/get_request/{param1}/{param2}")
    public ResponseEntity<\Object> getObjectDetails(@PathVariable String param1,@PathVariable String param2){
        return objectServiceBean.getObjectDetails(param1,param2);
    }

    @RequestMapping(value = "/post-request/",method=RequestMethod.POST)
    public ResponseEntity<Response> updateObjectDetails(@RequestBody String updateObjectString){
        JSONObject updateObject = null;
        Response response = new Response();
        Object object = new Object();
        try
        {
            updateObject = new JSONObject(updateObjectString);
        }
        catch(JSONException e)
        {
            response.setResponse("Incorrect JSON structure:" + e.toString());
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(response);
        }
        return objectServiceBean.updateObjectDetails(object);
    }
}

【问题讨论】:

  • 你用的是哪个spring版本,是3.2吗?

标签: json spring rest spring-mvc


【解决方案1】:

您还定义了以下配置,这与您的默认内容类型设置相矛盾,

<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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-23
    • 1970-01-01
    • 2020-08-18
    • 2019-10-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多