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>
source share