没有映射到我的请求[/]的引用方法allHotels()的映射
以下是我的Application和Controller类。
应用程序类
@SpringBootApplication(scanBasePackages = {"comexam.example"})
@EnableAutoConfiguration
public class Application
{
public static void main(String[] args) throws ParseException
{
SpringApplication.run(Application.class, args);
}
}
HotelController.class
@RestController
public class HotelController
{
@Autowired HotelService hotelService;
@RequestMapping(value = "/hotels/", method = RequestMethod.GET)
public ResponseEntity<List<Hotel>> allHotels() {
List<Hotel> hotels = hotelService.getAllHotels();
if (hotels.isEmpty()) {
return new ResponseEntity<List<Hotel>>(HttpStatus.NO_CONTENT);//You many decide to return HttpStatus.NOT_FOUND
}
return new ResponseEntity<List<Hotel>>(hotels, HttpStatus.OK);
}
}
以下是我得到的错误堆栈。
2017-04-19 17:17:23.729 INFO 5808 --- [ main] o.apache.catalina.core.StandardService : Starting service Tomcat
2017-04-19 17:17:23.730 INFO 5808 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.0.36
2017-04-19 17:17:23.836 INFO 5808 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2017-04-19 17:17:23.836 INFO 5808 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1646 ms
2017-04-19 17:17:24.077 INFO 5808 --- [ost-startStop-1] o.s.b.c.e.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]
2017-04-19 17:17:24.080 INFO 5808 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2017-04-19 17:17:24.081 INFO 5808 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2017-04-19 17:17:24.081 INFO 5808 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2017-04-19 17:17:24.081 INFO 5808 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
2017-04-19 17:17:24.277 INFO 5808 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@382db087: startup date [Wed Apr 19 17:17:22 IST 2017]; root of context hierarchy
2017-04-19 17:17:24.334 INFO 5808 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/hotels/],methods=[GET]}" onto public org.springframework.http.ResponseEntity<java.util.List<comexam.example.model.Hotel>> comexam.example.controller.HotelController.allHotels()
2017-04-19 17:17:24.337 INFO 5808 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2017-04-19 17:17:24.338 INFO 5808 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2017-04-19 17:17:24.365 INFO 5808 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-04-19 17:17:24.365 INFO 5808 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-04-19 17:17:24.402 INFO 5808 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-04-19 17:17:24.497 INFO 5808 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2017-04-19 17:17:24.582 INFO 5808 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
最佳答案
您已指定要扫描的软件包,因此您的控制器类必须位于“ it.exam.training”软件包内的某个位置。
@SpringBootApplication(scanBasePackages = {"it.exam.training"})
要解决此问题,请将HotelController类移至例如“ it.exam.training.controller”。