Spring 4 WebApplicationInitializer not being called

2.1k views Asked by At

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.

1

There are 1 answers

0
asifsid88 On

Make the use of AbstractAnnotationConfigDispatcherServletInitializer. This worked for me, So your code will look like below

public class HelloWorldInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

    protected Class<?>[] getServletConfigClasses() {
        return new Class<?>[] { HelloWorldConfiguration.class };
    }

    protected String[] getServletMappings() {
        return new String[] { "/" };
    }

    protected Class<?>[] getRootConfigClasses() {
        return null;
    }

    @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;
    }