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!
@Controllerwith@RestControllerAre you able to access a welcome page in your application? No root context? And use/*for url-pattern.GreetingConfiguration.