我正在尝试使用Spring创建一个简单的REST服务。基本查询中发生错误: HTTP 406不可接受:org.springframework.web.HttpMediaTypeNotAcceptableException:找不到可接受的表示形式
我有一个简单的控制器:
package com.test.my.rest;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping(value = "/sample", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class SampleRestController {
@GetMapping(value = "/test_string")
public String getString() {
return "Hello";
}
@GetMapping(value = "/test_string_wrap")
public ResponseEntity<String> getEntityString() {
return ResponseEntity.ok("Hello");
}
@GetMapping(value = "/test_int")
public Integer getInt() {
return 1;
}
@GetMapping(value = "/test_int_wrap")
public ResponseEntity<Integer> getInteger() {
return ResponseEntity.ok(new Integer(2));
}
@GetMapping(value = "/test_item")
public ResponseEntity<List<Item>> getItems() {
List<Item> list = new ArrayList();
list.add(new Item(1, "Item1"));
list.add(new Item(2, "Item2"));
return ResponseEntity.ok(list);
}
}
我尝试使用浏览器和curl工具调用服务:
curl -X GET --header "Accept: application/json" http://localhost:8070/rest/sample/test_int -v
或
http://localhost:8070/rest/sample/test_int
但是所有方法(../test_string和../test_string_wrap除外)都会引发此HTTP 406错误,甚至是基本的http://localhost:8070/rest/sample/test_int:
[[ACTIVE] ExecuteThread: '0' for queue: 'weblogic.kernel.Default (self-tuning)'] TRACE org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod ServletInvocableHandlerMethod.java:124 - Error handling return value=[1], type=java.lang.Integer in public java.lang.Integer com.test.my.rest.SampleRestController.getInt()
org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation
at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:306) ~[spring-webmvc-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.handleReturnValue(RequestResponseBodyMethodProcessor.java:180) ~[spring-webmvc-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.web.method.support.HandlerMethodReturnValueHandlerComposite.handleReturnValue(HandlerMethodReturnValueHandlerComposite.java:82) ~[spring-web-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:119) [spring-webmvc-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895) [spring-webmvc-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:800) [spring-webmvc-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) [spring-webmvc-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1038) [spring-webmvc-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942) [spring-webmvc-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1005) [spring-webmvc-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:897) [spring-webmvc-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:687) [javax.servlet.javax.servlet-api.jar:3.1.0]
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:882) [spring-webmvc-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790) [javax.servlet.javax.servlet-api.jar:3.1.0]
我的配置 web.xml :
<?xml version="1.0" encoding="UTF-8" ?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<context-param>
<param-name>contextClass</param-name>
<param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>com.test.my.config.SpringContextConfig</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextClass</param-name>
<param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</init-param>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>com.test.my.config.SpringDispatcherConfig</param-value>
</init-param>
<load-on-startup>99</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
配置 SpringDispatcherConfig :
@Configuration
@ComponentScan(basePackages = {"com.test.my.rest"})
public class SpringDispatcherConfig {
}
配置 pom.xml :
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
...
<properties>
<spring.version>5.1.3.RELEASE</spring.version>
<jackson.version>2.9.8</jackson.version>
<jxls.version>2.5.1</jxls.version>
<jxls-poi.version>1.1.0</jxls-poi.version>
</properties>
...
<dependencies>
...
<dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>${jackson.version}</version>
</dependency>
...
</dependencies>
</project>
weblogic 12.2.1的配置 weblogic.xml :
<?xml version = '1.0' encoding = 'windows-1252'?>
<weblogic-web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.bea.com/ns/weblogic/weblogic-web-app.xsd"
xmlns="http://www.bea.com/ns/weblogic/weblogic-web-app">
<context-root>/</context-root>
<container-descriptor>
<prefer-web-inf-classes>false</prefer-web-inf-classes>
<prefer-application-packages>
<package-name>org.slf4j.*</package-name>
<package-name>org.springframework.*</package-name>
<package-name>com.fasterxml.*</package-name>
</prefer-application-packages>
</container-descriptor>
</weblogic-web-app>
请帮助了解错误之处:(
答案 0 :(得分:0)
经过数千次尝试,数小时的尝试和错误,我找到了解决问题的方法。
在List>转换器中,Jackson转换器没有被拉到AbstractMessageConverterMethodProcessor构造函数中。
因为...应该在SpringDispatcherConfig /
处添加了@EnableWebMvc注释@Configuration
@EnableWebMvc // solve the problem
@ComponentScan(basePackages = {"com.bpcbt.iplan.rest"})
public class SpringDispatcherConfig {
}
答案 1 :(得分:-1)
尝试为所有API请求设置标头的接受类型。目前,我正在设置“ 本地”(仅针对您的一个API),但是您可以将其设置为“ 全局”。
@GetMapping(value = "/test_int_wrap")
public ResponseEntity<Integer> getInteger() {
HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders(Collections.singletonList(MediaType.APPLICATION_JSON_UTF8_VALUE)); try setting this media type.
return ResponseEntity
.ok(new Integer(2))
.headers(responseHeaders);
}