快生活 - 生活常识大全

项目搭建过程中遇到的错误点及解


  初学SpringBoot,此过程中遇到各种问题,这里做记录,以防后面忘记了
  1.要运行的application类启动时报:
  org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.
  at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:155) ~[spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
  at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:544) ~[spring-context-5.0.7.RELEASE.jar:5.0.7.RELEASE]
  at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140) ~[spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
  解决方案:
  未给要运行的application class添加注解 @EnableAutoConfiguration,再次启动没有该错误
  2.访问 http://localhost:8080/hello 页面报错:
  Whitelabel Error Page
  This application has no explicit mapping for /error, so you are seeing this as a fallback.
  Thu Jun 28 10:42:48 CST 2018
  There was an unexpected error (type=Internal Server Error, status=500).
  Error resolving template "hello springboot", template might not exist or might not be accessible by any of the configured Template Resolvers
  错误原因:由于application类使用了注解@Controller,而使用@Controller注解时需要有对应的页面存在,如jsp/html等,所以解决方式有如下两种:
  a.将注解@Controller换成 @RestController,再次访问没有该错误
  b.在templates下添加一页面,如下图
  application类中注解使用@Controller,sayHello方法返回该页面"index"
网站目录投稿:代巧