2

I am trying to create a Spring 4 RESTFul Service using Java Configuration and deploy to Tomcat. But I am not able to hit the endpoint. What am I missing? This is what I have.

I have a Greeting POJO that has setters and getters.

public class Greeting {
    private BigInteger id;
    private String text;

    //setters and getters
}

I have a Greeting Controller.

@Controller
public class GreetingController {

  private static BigInteger nextId;
  private static Map<BigInteger, Greeting> greetingMap;

  //have some code to store Greetings in greetingMap

  @RequestMapping(value = "/api/greetings", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
  public ResponseEntity<Collection<Greeting>> getGreetings() {
      Collection<Greeting> greetings = greetingMap.values();
      return new ResponseEntity<Collection<Greeting>>(greetings, HttpStatus.OK);
  }

}

I have a Configuration class.

@Configuration
@ComponentScan(basePackages = "org.example")
  public class GreetingConfiguration {

}

And this is my src/main/webapp/WEB-INF/web.xml

<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

  <display-name>Spring Application</display-name>

  <servlet>
      <servlet-name>dispatcher</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      <init-param>
          <param-name>contextClass</param-name>
          <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
      </init-param>
      <init-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>org.example.ws.configuration.GreetingConfiguration</param-value>
      </init-param>
      <init-param>
          <param-name>dispatchOptionsRequest</param-name>
          <param-value>true</param-value>
      </init-param>
      <load-on-startup>1</load-on-startup>
  </servlet>

  <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>org.example.ws.configuration.GreetingConfiguration</param-value>
  </context-param>

  <servlet-mapping>
      <servlet-name>dispatcher</servlet-name>
      <url-pattern>/</url-pattern>
  </servlet-mapping>

  <listener>
      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

I deployed to tomcat. Tomcat starts ok. I tried to hit it http://localhost:8080/api/greetings , it gives me 404. What am I missing?

Thanks!

2
  • Try replacing @Controller with @RestController Are you able to access a welcome page in your application? No root context? And use /* for url-pattern. Commented Nov 23, 2016 at 9:27
  • i think you have problem in GreetingConfiguration. Commented Nov 23, 2016 at 9:33

2 Answers 2

1

You are combining XML and Java configuration. So, you need to tell Spring to pick up controller annotations by one of the two methods:

  1. @EnableWebMvc in your configuration class
  2. Or, in XML
Sign up to request clarification or add additional context in comments.

Comments

0

I think you need to add @ResponseBody to your rest method

 @Controller
    public class GreetingController {

      private static BigInteger nextId;
      private static Map<BigInteger, Greeting> greetingMap;

      //have some code to store Greetings in greetingMap

      @RequestMapping(value = "/api/greetings", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
      @ResponseBody
      public ResponseEntity<Collection<Greeting>> getGreetings() {
          Collection<Greeting> greetings = greetingMap.values();
          return new ResponseEntity<Collection<Greeting>>(greetings, HttpStatus.OK);
      }

    }

and check that you have jackson databinding dependency in your pom.xml

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.