这是我的应用程序配置类:
@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.
配置程序时,有人可以帮助解决问题吗?
答案 0 :(得分:1)
更改
@RequestMapping("/user")
public class UserController
到
@RequestMapping("/")
public class UserController