HttpMediaTypeNotAcceptableException: could not find acceptable spring mvc 4.1.5 view with com.fasterxml 2.5.1

Help - what else can I do to fix this error? org.springframework.web.HttpMediaTypeNotAcceptableException :: Could not find an acceptable view.

I think my project is set up correctly to handle json restful requests. In the past few days, I have read and applied various sentences to no avail. Any other ideas on what else I should do differently?

I am using Spring MVC 4.1.5 and com.fasterxml 2.5.1.

Part of my pom.xml

<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.5.1</version> <type>bundle</type> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.5.1</version> <type>bundle</type> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.5.1</version> <type>bundle</type> </dependency> 

Here is my part of my web.xml

 <!-- Creates the Spring Container shared by all Servlets and Filters --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- Processes application requests --> <servlet> <servlet-name>appServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value> /WEB-INF/spring/appServlet/servlet-context.xml </param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>appServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> 

Here is part of my servlet-context.xml

 <!-- Enables the Spring MVC @Controller programming model --> <annotation-driven /> <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory --> <resources mapping="/resources/**" location="/resources/" /> <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory --> <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <beans:property name="prefix" value="/WEB-INF/views/" /> <beans:property name="suffix" value=".jsp" /> </beans:bean> <context:component-scan base-package="com.losgatos"> <context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice" /> </context:component-scan> 

Here is my controller

 package com.losgatos.controllers; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.losgatos.User; @RestController @RequestMapping( "user" ) public class UserController { @RequestMapping( value = "data", produces="application/json" ) public User getUser(){ return new User(); } } 

Here is the POJO user

 package com.losgatos; import java.util.EnumSet; public class User { public User(){ id = 0; age = 22; name = "Titus Feng"; alias = "tornado tie"; roles = EnumSet.of( Role.NORMAL, Role.NEW ); } private int id, age; private String name, alias; private EnumSet<Role> roles; //added getters and setters here public enum Role{ NEW, NORMAL, ADMIN, MEMBER, DORMANT } } 
+5
source share
3 answers

I got it to work finally:
removing <type>bundle</type> from my quickxml.jackson pom.xml files
restart my pom.xml file
project cleaning (project → clean)
cleaning and restarting the server

+1
source

Spring Framework 4.1 raised some dependency requirements ; you need to upgrade Jackson to version 2.3 or later. Note that the groupId artifact has changed to com.fasterxml .

+1
source

In your spring configuration config file (dispatcher-servlet.xml) you have:

 <context:component-scan base-package="com.losgatos"> <context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice" /> </context:component-scan> 

and your controller is annotated with:

 @RestController @RequestMapping( "user" ) public class UserController { ... } 

your controller class will not be fully loaded into the container. BTW according to your exception message:

I get an error

org.springframework.web.HttpMediaTypeNotAcceptableException :: Could not find an acceptable representation.

This exception occurs if you did not register the following in the context file:

  • <mvc:annotation-driven /> (you are not registered in your context file yet)
  • If a JAR is required, there is no class path (you already have)
  • When using ENUM, annotate your ENUM class with @JsonFormat(shape= JsonFormat.Shape.OBJECT)

EDIT: works fine for me, with spring 4.0.1.RELEASE and below a single JSON dependency:

com.fasterxml.jackson.core JACKSON-DataBind 2.4.3

and controller class:

 @RestController @RequestMapping("/u") public class UserRestController { @RequestMapping( value = "/data", produces="application/json" ) public RestUser getUser(){ return new RestUser(); } } 

got:

 {"id":0,"age":22,"name":"Titus Feng","alias":"tornado tie","roles":["NEW","NORMAL"]} 

Suggestion, change this:

 <context:component-scan base-package="com.losgatos"> <context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice" /> </context:component-scan> 

in

 <context:component-scan base-package="com.losgatos"> <context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.RestController" /> </context:component-scan> 
0
source

Source: https://habr.com/ru/post/1214873/


All Articles