在DispatcherServlet中找不到带有URI [/ CalorieTracker /]的HTTP请求的映射,其中包含名称' dispatcher'

时间:2016-11-11 03:41:26

标签: spring spring-mvc

这是我的应用程序配置类:

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.spring")
public class AppConfig extends WebMvcConfigurerAdapter{

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

}

这是我的应用程序配置初始化程序类。 公共类AppConfigInitializer扩展AbstractAnnotationConfigDispatcherServletInitializer {

@Override
protected Class<?>[] getRootConfigClasses() {
    // TODO Auto-generated method stub
    return new Class[] {AppConfig.class};
}

@Override
protected Class<?>[] getServletConfigClasses() {
    // TODO Auto-generated method stub
    return null;
}

@Override
protected String[] getServletMappings() {
    // TODO Auto-generated method stub
    return new String[] {"/"};
}

}

这是我的休息控制器类。

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

    private UserService userService;

@RequestMapping(method = RequestMethod.POST)
    public ResponseEntity<UserResource> createUser(@RequestBody UserResource userDetails) {
        try {
            User createdUser = userService.createUser(userDetails.toUser());
            UserResource res = new UserResourceAsm().toResource(createdUser);
            HttpHeaders headers = new HttpHeaders();
            headers.setLocation(URI.create(res.getLink("self").getHref()));
            return new ResponseEntity<UserResource>(res, headers,  HttpStatus.CREATED);
        } catch (UserExistsException e) {
            throw new ConflictException(e);
        }
    }
}

在控制台中我收到此警告

WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/CalorieTracker/] in DispatcherServlet with name 'dispatcher'

我相信因为这个警告,当我试图打电话给我的休息api&#34; http://localhost:8080/CalorieTracker/user&#34;时,我的状态为404.

配置程序时,有人可以帮助解决问题吗?

1 个答案:

答案 0 :(得分:1)

更改

@RequestMapping("/user")
public class UserController

@RequestMapping("/")
public class UserController