我有一個Spring MVC 3.2應用程序不接受來自瀏覽器的JSON POST請求。如果我使用CocoaRest等工具,一切都很好。但是,當我使用瀏覽器或工具,如鉻://restclient/content/restclient.html然後我得到,因爲請求 實體是不支持的格式SpringMVC JSON不受支持的類型
HTTP狀態415 -The服務器拒絕這個請求請求的資源爲 請求的方法。
這是我的配置:
@Bean
public ContentNegotiationManagerFactoryBean contentNegotiationManager() {
Properties properties = new Properties();
properties.setProperty("xml", "application/xml");
properties.setProperty("json", "application/json");
properties.setProperty("html", "application/html");
ContentNegotiationManagerFactoryBean contentNegotiationManager = new ContentNegotiationManagerFactoryBean();
contentNegotiationManager.setFavorParameter(true);
contentNegotiationManager.setMediaTypes(properties);
contentNegotiationManager.setIgnoreAcceptHeader(true);
contentNegotiationManager.setDefaultContentType(MediaType.APPLICATION_JSON);
return contentNegotiationManager;
}
和我的POM:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.0.4</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.0.4</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
這是我的控制器:
@Transactional
@ResponseBody
@RequestMapping(method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)public EmployeeDTO addEmployee(@PathVariable String officeName, @RequestBody @Valid final Employee employee,
BindingResult result) {
List<String> errors = new ArrayList<String>();
if (result.hasErrors()) {
for (ObjectError error : result.getAllErrors()) {
errors.add(error.getDefaultMessage());
}
throw new EmployeeSetupException(errors, "Employee could not be configured, please check your submitted values.");
}
...
}
這是我的請求:
{
"address" : {
"status" : null,
"city" : "Lakewood",
"addressId" : 1,
"seqId" : null,
"addressLine1" : "123 Maple Ave",
"zip" : "80111",
"addressLine2" : "Apt 101",
"zipLong" : null,
"addressLine3" : "room C ",
"state" : "CO"
},
"office" : null,
"licenseNumber" : "0987654321A",
"scheduleId" : null,
"user" : {
"status" : 1,
"userName" : "[email protected]",
"userRole" : {
"userRoleDescription" : "The person or entity that owns a particular practice, who has the highest level of authorization",
"userRoleDescriptionShort" : "Practice Owner",
"userRoleId" : 20
}
},
"firstName" : "Sid",
"ssn" : "111-22-3332",
"specialtyId" : null,
"lastName" : "Vicious",
"fullName" : null
}
我讀了一些文章,說將標題內容設置爲應用程序/ json或表單將做到這一點。但是,當我將contentType設置爲application/json和/或application/x-www-form-urlencoded時,它仍然在瀏覽器中失敗。我也嘗試通過jQuery提交,但我得到了相同的結果。
,如果我改變控制器從:
@RequestMapping(method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
到
@RequestMapping(method = RequestMethod.POST)
然後我得到這個,當我提交直通瀏覽器:
Received Exception Content type 'text/plain;charset=UTF-8' not supported
org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'text/plain;charset=UTF-8' not supported
at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodArgumentResolver.readWithMessageConverters(AbstractMessageConverterMethodArgumentResolver.java:149)
at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.readWithMessageConverters(RequestResponseBodyMethodProcessor.java:180)
at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.resolveArgument(RequestResponseBodyMethodProcessor.java:95)
at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:77)
at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:162)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:123)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:745)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:686)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:936)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:838)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
這沒有意義因爲Chrome rest客戶端chrome://restclient/content/restclient.html顯示請求的application/json的contentType。
如果有人知道什麼可能會出錯,我當然會很感激幫助。
你有沒有嘗試設置(在CONF文件):'contentNegotiationManager.setIgnoreAcceptHeader(假);' – nickdos
是的,我改變了 'contentNegotiationManager.setIgnoreAcceptHeader(假)' 到 'contentNegotiationManager.setIgnoreAcceptHeader(真)',但我仍然得到相同的'415內容消息' – sonoerin
我建議它應該是false(你的代碼如上所述它是真實的) - 你希望它使用accept頭並且不依賴文件擴展來檢測內容類型(當設置爲真正)。但這聽起來像是你用兩個值來試驗它。 – nickdos