-1
沒有映射這是我的應用程序的配置類:在DispatcherServlet的發現HTTP請求與URI [/ CalorieTracker /]名爲「調度」
@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'
我相信,因爲這個警告,當我試圖打電話給我的REST API「http://localhost:8080/CalorieTracker/user」,我正在狀態404
任何人都可以在配置程序時幫助解決問題嗎?
在映射添加'caloieTracker'如'@RequestMapping(值= 「CalorieTracker」,方法...)'和自移除'user'映射你沒有附加並返回頁面 – emotionlessbananas
不,這是行不通的。你能告訴我爲什麼我得到那個警告「找不到HTTP請求的映射」嗎? – bthapa
閱讀本文以供參考(http://www.baeldung.com/spring-requestmapping) – emotionlessbananas