添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Spring REST service throw "406 Not Acceptable": org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation

Ask Question

I'm trying to create a simple REST service with Spring. Errors occur in elementary queries: HTTP 406 Not Acceptable: org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation

I have a simple controller:

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

I tried to call the services using the browser and the curl utility:

curl -X GET --header "Accept: application/json" http://localhost:8070/rest/sample/test_int -v
http://localhost:8070/rest/sample/test_int

But all methods (except ../test_string and ../test_string_wrap) throw this HTTP 406 error, even elementary 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]

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

Configuration SpringDispatcherConfig:

@Configuration
@ComponentScan(basePackages = {"com.test.my.rest"})
public class SpringDispatcherConfig {

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

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

Please help to understand what is wrong :(

MediaType.APPLICATION_JSON_UTF8_VALUE is not equal to application/json. Did you try setting the ACCEPT header to application/json;charset=UTF-8 ? I don't know if Spring should be resolving this on its own though – Abaddon666 Mar 28, 2019 at 8:30 yes, I tried to invoke as "curl -X GET --header "Accept: application/json;charset=UTF-8" localhost:8070/rest/sample/test_int -v" but result is same: HTTP/1.1 406 Not Acceptable – Sorand Mar 28, 2019 at 8:35 Does test_string work? If yes it the reason might be the return value? So something is wrong with jackson? – DCO Mar 28, 2019 at 8:52 Headers: > GET /rest/sample/test_int HTTP/1.1 > Host: localhost:9999 > User-Agent: curl/7.64.1 > Accept: application/json > < HTTP/1.1 406 Not Acceptable < Date: Thu, 28 Mar 2019 08:56:50 GMT < Content-Length: 0 – Sorand Mar 28, 2019 at 8:57

After thousands trial, hours and errors I found a solution to the problem.

Jackson converters are not not pulled up to AbstractMessageConverterMethodProcessor constructor in List> converters...

Because ... should have added an annotation @EnableWebMvc at SpringDispatcherConfig/

@Configuration
@EnableWebMvc  // solve the problem
@ComponentScan(basePackages = {"com.bpcbt.iplan.rest"})
public class SpringDispatcherConfig {

Try setting your header's accept-type for all your API requests. Currently, I'm setting 'locally' ( for only one of your APIs) but you can set it 'globally'.

@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);
                Your API is not supported by my spring version. Try to as: 		MultiValueMap<String, String> headers = new HttpHeaders(); 		headers.put(HttpHeaders.ACCEPT, Collections.singletonList(MediaType.APPLICATION_JSON_VALUE)); 		return new ResponseEntity(new Integer(2), headers, HttpStatus.OK); But error is remained.
– Sorand
                Mar 28, 2019 at 10:03
                make MediaType to APPLICATION_JSON_UTF8_VALUE instead of APPLICATION_JSON_VALUE in your above comment, and look if it works.
– Jabongg
                Mar 28, 2019 at 10:05
                Done and tried with curl -X GET --header "Accept: application/json;charset=UTF-8" http://localhost:9999/rest/sample/test_int -v and curl -X GET --header "Accept: application/json" http://localhost:9999/rest/sample/test_int -v.It didn't help :(
– Sorand
                Mar 28, 2019 at 10:22
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.