2

AppInitailizer.java

public class AppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer{

@Override
protected Class<?>[] getRootConfigClasses() {
    return new Class[]{AppConfig.class};
}

@Override
protected Class<?>[] getServletConfigClasses() {
    return null;
}

@Override
protected String[] getServletMappings() {
    return new String[]{"/api/*"};
}

}

AppConfig.java

@Configuration
@ComponentScan
@EnableWebMvc
public class AppConfig {

}

UserController.java

@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    private UserService userService;


    @RequestMapping(method = RequestMethod.GET)
    public List<User> getUsers(){
        List<User> users = userService.findAll();
        return users;
    }

When I tried to fire http://localhost:8080/practise/api/users, it results in "HTTP Status 404" - /practice/api/users (where practice is project name). In getServletMappings(), I gave the path as /api/* and UserController class has request mapping as /users. I dont know where I got wrong. When I access index.xml, it is working fine.

Screenshot of error:

Error on the browser when I try to access the api of application

halfer
  • 19,824
  • 17
  • 99
  • 186
subbu ch
  • 61
  • 3
  • When I fire URL there is no error in the tomcat logs saying, there is no mapping for given URL. I tried to print a statement getRootConfigClasses(), getServletMappings(). The print statement is missing in the logs as well. So I assume, configuration of the spring mvc might be wrong. But how could I figure it out ? – subbu ch Mar 10 '16 at 18:47

1 Answers1

0

you should have to use value attribute in Requestmapping like @RequestMapping(value="users",method = RequestMethod.GET)

  • -> I gave the @RequestMapping("/users") to class UserController . As RequestMapping is hierarchial, it applies to function getUsers(). Please correct me if I am wrong. – subbu ch Mar 10 '16 at 18:42
  • yes you are right but to differnsiate what method has to handle request you should give @RequestMapping(value="something") and you have to call by full URL path like yourpath/users/something .i think it will work for you – santhosh sarakadam Mar 11 '16 at 05:45