0
添加以下到我的servlet-config.xml
並擺脫了推杆.json
後綴在Spring MVC中的GET請求REST API的結束,但該職位API我仍然需要.json
到底如何在REST API發佈請求結束時避免後綴?
<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);
}
}
這春天你使用的版本是3.2嗎? – ScanQR