0

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;
  }
}
  • The 404 means that the RequestMapping you are trying to access is not available. You can go to logs and check request mappings there or if nothing was printed to log you can try to do this programmatically: https://stackoverflow.com/questions/10898056/how-to-find-all-controllers-in-spring-mvc/10899118#10899118 – nikita_pavlenko Jun 03 '17 at 13:09
  • I checked in the logs. The resource is getting mapped there. But still when I try to access the resource, 404 error is getting displayed. – Rahul Chandwani Jun 03 '17 at 13:12
  • Try POST it via a broswer? Check to see Accept content-type set? – Minh Kieu Jun 03 '17 at 13:17
  • @RahulChandwani have you tried to send request via Postman e.g. with Header "Accept application/json" ? – nikita_pavlenko Jun 03 '17 at 14:57
  • @nikita_pavlenko Yes I did. Still getting the same error. – Rahul Chandwani Jun 03 '17 at 16:16

2 Answers2

0

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.

eg04lt3r
  • 2,467
  • 14
  • 19
  • I tried adding the Session class but still the same error. The problem was the servlet mapping which I have mentioned in the answer. – Rahul Chandwani Jun 04 '17 at 05:51
0

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("/*");

  • Actually can you explain what is difference between xml and annotations configuration for application? It is something new for me. – eg04lt3r Jun 04 '17 at 19:36
  • Please refer to the following answer for the difference between XML and annotation configuration. https://stackoverflow.com/questions/8428439/spring-annotation-based-di-vs-xml-configuration – Rahul Chandwani Jun 05 '17 at 08:44
  • Thanks for the link. But I do not see any effect on mapping of servlet. Maybe I missed something? – eg04lt3r Jun 05 '17 at 10:55