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
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 :(
–
–
–
–
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);
–
–
–
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.