0

Even though I´ve seen many questions about this issue, no one has been usefull to me. I´m trying to publish a simple REST service but not matters what I do, it is not working.

My very first approach was to create a REST controller like this:

@RestController
public class ProductsRestController {

    @Autowired
    private ProductService  productService;

    @Transactional
    @RequestMapping(value = "/listproducts")
    public List<Product> products () {
        return this.productService.findAll();
    }
}

When invoking it (through browser) a HttpMediaTypeNotAcceptableException was thrown

Googling I reached this Question. The solution proposed, is to configure a MappingJacksonHttpMessageConverter and add it to the AnnotationMethodHandlerAdapter. As these beans are deprecated in Spring 4.x, I used the RequestMappingHandlerAdapter with MappingJackson2HttpMessageConverter this way (within my WebMvcConfigurerAdapter):

@Override
public void configureMessageConverters (final List<HttpMessageConverter<?>> converters) {
    converters.add(new MappingJackson2HttpMessageConverter());
}

This conduced me to the following error:

java.lang.ClassNotFoundException: com.fasterxml.jackson.core.JsonProcessingException

I added the needed dependencies

<artifactId>jackson-core</artifactId>
<artifactId>jackson-databind</artifactId>

and that result on:

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.IllegalStateException: getOutputStream() has already been called for this response
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:978)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:857)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
    org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:118)
    org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:84)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:103)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:113)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:154)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:45)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:199)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:110)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.csrf.CsrfFilter.doFilterInternal(CsrfFilter.java:85)
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:57)
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:50)
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:192)
    org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:160)
    org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:344)
    org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:261)

Googling again I reached this other Question But neither this one was useful.

Does anyone have any idea what the underlying problem?

Thanks in advance! Regards.

Community
  • 1
  • 1
Emiliano Schiano
  • 1,862
  • 1
  • 25
  • 30
  • 1
    Add the proper jackson version to your classpath, add `@EnableWebMvc` and leave the rest to Spring. You don't need to mess around with adding either `HandleAdapter` or custom `HttpMessageConverter`s. – M. Deinum Jan 14 '15 at 14:14
  • You re right. It seems that just addind jackson deps, Spring automatically configures the handlers. Despite of this, the same error (getOutputStream() has already been called) still happens. – Emiliano Schiano Jan 14 '15 at 14:18
  • Do you have custom filters configured that read the request/response? – M. Deinum Jan 14 '15 at 14:20

1 Answers1

0

Whatever the problem is, there is always further googling to do. This other Question helped to solve my problem.

The thing is that the Product class has references to other beans that reference to Product again, so a circular reference is happening. Using DTO solved the problem.

Thanks to everyone for your support.

Community
  • 1
  • 1
Emiliano Schiano
  • 1,862
  • 1
  • 25
  • 30