Maria Speicher
Maria Speicher

Reputation: 123

Errors using Spring 4.1 @RestController

I'm trying to return JSON when using the @RestController. I'm using Spring 4.1. Here's the exception I'm getting when calling listrestsites.html using a GET request. I have the fasterxml Jackson core and databind jars in my build path. Output of the accept from @requestheader = accept: application/json, text/javascript, /; q=0.01 Any help is appreciated. Thank you,

[DEBUG,ExceptionHandlerExceptionResolver] Resolving exception from handler [public java.util.List com.amci.spring3.controller.SitesRestController.listRestSites(java.lang.String)]: org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation [DEBUG,DefaultListableBeanFactory] Returning cached instance of singleton bean 'exceptionControllerAdvice' [DEBUG,ExceptionHandlerExceptionResolver] Invoking @ExceptionHandler method: public org.springframework.web.servlet.ModelAndView

Here's my Restcontroller class:

@RestController
public class SitesRestController {

    @Autowired
    private AssetService assetService;


    @RequestMapping("/listrestsites.html") 
    public  List<Asset> listRestSites(@RequestHeader(value="accept") String accept) {

        System.out.println(getLogLevel());
        System.out.println("accept: " + accept);
        return assetService.findAssets();
    }

}

Also, snippet from my spring.xml:

  <property name="defaultViews">
    <list>
      <!-- JSON View -->
      <bean
        class="org.springframework.web.servlet.view.json.MappingJackson2JsonView">
      </bean>

     </list>
  </property>
  <property name="ignoreAcceptHeader" value="true" />

</bean>

Upvotes: 0

Views: 534

Answers (1)

ankit
ankit

Reputation: 5127

Please make sure that you have the following in your Spring xml file:

<context:annotation-config/> 

<bean id="jacksonMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"></bean>
 <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
  <list>
    <ref bean="jacksonMessageConverter"/>
  </list>
</property>
</bean>

and all items of your POJO should have getters/setters. Hope it helps

Credit answer to this question

Upvotes: 1

Related Questions