Search code examples
springspring-mvcspring-restcontroller

Following Spring 5 webmvc Rest service guide - No converter found for return value of type


I am following the official guide for Building a RESTful Web Service from Spring.io

In short, I'm following the above guide but using Tomcat to deploy and execute.

I've looked at some SO questions and answers regarding this issue. Spring Boot Application: No converter found for return value of type

and some others.

I have the getter methods in the Greeting class though I don't have setters. This is exactly how the class is on the guide.

I tried adding the fasterxml json dependency on my pom.xml but the error message is the same.

All my setup is exactly the same as the guide except the app is bootstrapped by a dispatch-servlet.xml instead of a main method.

web.xml

    <servlet>
    <servlet-name>greeting</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>greeting</servlet-name>
    <url-pattern>/greeting/*</url-pattern>
  </servlet-mapping>

greeting-servlet.xml

<context:component-scan base-package="com.test" />

This is it. All the other classes are written exactly the same as the guide. with @RestController and no @ResponseBody as @RestController is rolled out with @Controller and @ResponseBody according to the guide itself.

This is my error message.

WARNING: Failed to write HTTP message: org.springframework.http.converter.HttpMessageNotWritableException: No converter found for return value of type: class com.test.Greeting

I don't think it's the issue with getters( I definitely have them). Nor it's the issue with not having the fasterxml dependency.

What am I missing?


Solution

  • You definitely need the main method:

    @SpringBootApplication
    public class Application {
    
        public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
        }
    }
    

    The @SpringBootApplication annotation does more than you think. The most important thing is that it calls@EnableAutoConfiguration. This is used to figures out which other dependencies you do in your project. In your case that annotation would configure Jackson to parse your Java object to JSON.

    Directly from the JavaDoc:

    Enable auto-configuration of the Spring Application Context, attempting to guess and configure beans that you are likely to need. Auto-configuration classes are usually applied based on your classpath and what beans you have defined. For example, If you have tomcat-embedded.jar on your classpath you are likely to want a TomcatEmbeddedServletContainerFactory (unless you have defined your own EmbeddedServletContainerFactory bean).


    If you still think creating a war file is a good idea, check if this can help you setup Jackson with plain Spring.


    EDIT: How to run Spring Boot without Tomcat
    You could check this out, the doc says to do this:

        ..
        <packaging>war</packaging> 
        ..
    
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
    

    And in theory you should be good to go, BUT that might not be the case so check out the sample project from Spring.

    These people also got some issues, so check out if it also effects you.