here is my code:
public class HelloWorldInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
// TODO Auto-generated method stub
System.out.println(">>>>>>>>>>>>>>>>>>>>>>> ONSTARTUP <<<<<<<<<<<<<<<<<<<<<<<<");
WebApplicationContext context = getConText();
servletContext.addListener(new ContextLoaderListener(context));
System.out.println(">>>>>>>>>>>>>>>>>>>>>>> ONSTARTUP END <<<<<<<<<<<<<<<<<<<<<<<<");
}
private AnnotationConfigWebApplicationContext getConText() {
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.setConfigLocation("com.websystique.springmvc.configuration.HelloWorldConfiguration");
return context;
}
The config:
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.websystique.springmvc")
public class HelloWorldConfiguration {
}
The rest controllers:
@RestController
public class HelloWorldRestController {
@Autowired
UserService userService; //Service which will do all data retrieval/manipulation work
//-------------------Retrieve All Users--------------------------------------------------------
@RequestMapping(value = "/user/", method = RequestMethod.GET)
public ResponseEntity<List<User>> listAllUsers() {
List<User> users = userService.findAllUsers();
if(users.isEmpty()){
return new ResponseEntity<List<User>>(HttpStatus.NO_CONTENT);//You many decide to return HttpStatus.NOT_FOUND
}
return new ResponseEntity<List<User>>(users, HttpStatus.OK);
}
JAR FILES:
spring-expression-4.2.0.RELEASE.jar"
spring-tx-4.2.0.RELEASE.jar"
spring-web-4.2.0.RELEASE.jar"
spring-webmvc-4.2.0.RELEASE.jar"
aopalliance-1.0.jar"
jackson-databind-2.5.3.jar"
spring-beans-4.2.0.RELEASE.jar"
spring-context-4.2.0.RELEASE.jar"
spring-context-support-4.2.0.RELEASE.jar"
spring-core-4.2.0.RELEASE.jar"
When I run http://localhost:9080/MyApp/user it says not found.
Is there something I forgot to do here? or have I done wrong?
I could not see the sysout logs I created in the onStartUp method so that means the HelloWorldInitializer cannot be seen, which in turn causes me not to be able to access the rest web service methods.
Make the use of
AbstractAnnotationConfigDispatcherServletInitializer
. This worked for me, So your code will look like below