Rahul Chandwani
Rahul Chandwani

Reputation: 61

Spring MVC Resource not found 404

I am getting 404 response not found when I try to call a RESTful Web Service from AngularJS. Following is the code sample :

WebAppConfiguration.java

public class WebAppConfiguration extends WebMvcConfigurerAdapter {

    @Override
    public void configureViewResolvers(ViewResolverRegistry registry) {
       InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
       viewResolver.setViewClass(JstlView.class);
       viewResolver.setPrefix("/WEB-INF/");
       viewResolver.setSuffix(".html");
       registry.viewResolver(viewResolver);
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
       registry.addResourceHandler("/app/**").addResourceLocations("/app/");
    }
}

WebAppInitializer.java

public class WebAppInitializer implements WebApplicationInitializer {
    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {  
        AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();        
        ctx.register(WebBeanConfig.class);
        ctx.setServletContext(servletContext);

        DispatcherServlet dispatcherServlet = new DispatcherServlet(applicationContext);
        ServletRegistration.Dynamic servlet = servletContext.addServlet("mvc-dispatcher", dispatcherServlet);
        servlet.addMapping("/");
        servlet.setAsyncSupported(true);
        servlet.setLoadOnStartup(1);
    }
}

WebBeanConfig.java

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.rvc.releaseSessionDemo")
public class WebBeanConfig {
}

SessionController.java

@RestController
@RequestMapping("/session")
public class SessionController {
   @Autowired
   private ISessionService sessionService;

   @RequestMapping(value = "/releaseSession", method = RequestMethod.POST,  produces = MediaType.APPLICATION_JSON_VALUE)
   public ResponseEntity<Boolean> releaseSession(@RequestBody String sessionId) throws Exception {
       Boolean response = sessionService.releaseSession(sessionId());
       return new ResponseEntity<Boolean>(response, HttpStatus.OK);
   }
}

session.service.js

angular.module('session', []).factory('sessionService', ['$http', '$q', sessionService]);

function sessionService($http, $q) {
   return {
      releaseSession : releaseSession
   }

function releaseSession(sessionId) {
    var deferred = $q.defer();
    var req = {sessionId : sessionId};
    $http.post('session/releaseSession', req).then(function(response) {
        deferred.resolve(response.data);
    }, function(errorResponse) {
        deferred.reject(errorResponse);
    });
    return deferred.promise;
  }
}

Upvotes: 0

Views: 2185

Answers (2)

Rahul Chandwani
Rahul Chandwani

Reputation: 61

I would myself like to answer the question.

The problem was the servlet mapping i.e. "/" which works only in case of XML based configuration. If we are using Annotation based configuration, then servlet mapping should be "/*" in the initializer class.

servlet.addMapping("/*");

Upvotes: 0

eg04lt3r
eg04lt3r

Reputation: 2610

You shoud use wrapper class for @ResponseBody.

Use something like this:

public class Session {
    private String sessionId;
    //getter setter
}

And pass it into controller @RequestBody Session session. Then you can get your session id by getter.

Upvotes: 0

Related Questions