0

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>
a better oliver
  • 26,330
  • 2
  • 58
  • 66

1 Answers1

1

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

Community
  • 1
  • 1
ankit
  • 4,919
  • 7
  • 38
  • 63
  • Thank you very much for the answer. I added in above couple weeks ago, and fixed the issue. – Maria Speicher Apr 28 '15 at 14:34
  • @MariaSpeicher whenever you find answers to your own question, please update it on StackOverflow it will help future users – ankit Apr 30 '15 at 06:52