【问题标题】:See nothing but white label error page - Spring boot + hibernate只看到白标错误页面 - Spring boot + hibernate
【发布时间】:2017-04-19 12:05:15
【问题描述】:

没有映射到我的请求 [/] 引用方法 allHotels() 以下是我的 Application 和 Controller 类。

Application.class

@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 包里吗?
  • 那么您想使用“/”映射访问 allHotels() 吗?
  • 视图解析器呢?

标签: java spring hibernate servlets spring-boot


【解决方案1】:

您已指定要扫描的 包,因此您的控制器类必须位于“it.exam.training”包中的某个位置。

@SpringBootApplication(scanBasePackages = {"it.exam.training"})

要解决此问题,请将 HotelController 类移动到例如“it.exam.training.controller”。

【讨论】:

  • 检查它们是否被正确扫描。您可以通过从 SpringApplication.run(Application.class, args) 检索应用程序上下文然后在其上调用 .getBeanDefinitionNames() 来实现。
  • HotelController 类在该 bean 定义列表中
  • 好的。尝试删除 @EnableAutoConfiguration 因为它已经包含在 SpringBootApplication 注释中。这可能是原因。
  • 试过了,但没有成功
  • :(您也可以发布您的 application.properties。我们可以在那里找到一些东西。
【解决方案2】:

将您的 Applicaiton.class 移至 it.example.training 包并将您的控制器移至 it.example.training.controller 再试一次。

【讨论】:

  • 您是否尝试将 value="/hotels/" 更改为 "/hotels"
猜你喜欢
  • 2016-08-04
  • 2023-01-29
  • 1970-01-01
  • 1970-01-01
  • 2022-12-23
  • 2017-06-03
  • 1970-01-01
  • 1970-01-01
  • 2016-06-19
相关资源
最近更新 更多