这个问题要从“
java.io.IOException: Server returned HTTP response code: 403 for URL: http://start.spring.io
”错误说起, eclipse 创建 spring boot 项目 遇到错误:java.io.IOException: Server returned HTTP response code: 403 for URL: http://start.spring.io然后就在网页上创建项目下载,新建Maven工程,修改Pom.xml 文件……
完整错误信息如下:
2017-12-29 17:28:51.149 INFO 4429 --- [ main] com.wanyu.fams.App : Starting App on avenMac.local with PID 4429 (/Users/aven/Documents/workspace/fams/fams/target/classes started by aven in /Users/aven/Documents/workspace/fams/fams) 2017-12-29 17:28:51.153 INFO 4429 --- [ main] com.wanyu.fams.App : No active profile set, falling back to default profiles: default 2017-12-29 17:28:51.228 INFO 4429 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@4e7912d8: startup date [Fri Dec 29 17:28:51 CST 2017]; root of context hierarchy 2017-12-29 17:28:52.062 WARN 4429 --- [ main] o.m.s.mapper.ClassPathMapperScanner : No MyBatis mapper was found in '[com.wanyu.fams]' package. Please check your configuration. 2017-12-29 17:28:53.358 INFO 4429 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http) 2017-12-29 17:28:53.384 INFO 4429 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2017-12-29 17:28:53.386 INFO 4429 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.23 2017-12-29 17:28:53.564 INFO 4429 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2017-12-29 17:28:53.564 INFO 4429 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 2341 ms 2017-12-29 17:28:53.904 INFO 4429 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/] 2017-12-29 17:28:53.913 INFO 4429 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*] 2017-12-29 17:28:53.914 INFO 4429 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*] 2017-12-29 17:28:53.915 INFO 4429 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*] 2017-12-29 17:28:53.915 INFO 4429 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*] 2017-12-29 17:28:54.580 INFO 4429 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@4e7912d8: startup date [Fri Dec 29 17:28:51 CST 2017]; root of context hierarchy 2017-12-29 17:28:54.725 INFO 4429 --- [ 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-12-29 17:28:54.727 INFO 4429 --- [ 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-12-29 17:28:54.805 INFO 4429 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2017-12-29 17:28:54.805 INFO 4429 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2017-12-29 17:28:54.929 INFO 4429 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2017-12-29 17:28:55.030 WARN 4429 --- [ main] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Tomcat.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.tomcat.jdbc.pool.DataSource]: Factory method 'dataSource' threw exception; nested exception is org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Cannot determine embedded database driver class for database type NONE. If you want an embedded database please put a supported one on the classpath. If you have database settings to be loaded from a particular profile you may need to active it (no profiles are currently active). 2017-12-29 17:28:55.034 INFO 4429 --- [ main] o.apache.catalina.core.StandardService : Stopping service [Tomcat] 2017-12-29 17:28:55.064 INFO 4429 --- [ main] utoConfigurationReportLoggingInitializer : Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled. 2017-12-29 17:28:55.072 ERROR 4429 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPLICATION FAILED TO START *************************** Description: Cannot determine embedded database driver class for database type NONE Action: If you want an embedded database please put a supported one on the classpath. If you have database settings to be loaded from a particular profile you may need to active it (no profiles are currently active).我认为错误产生的原因是:创建项目的时候添加了 MyBatis 依赖,但却没有添加对应的数据库配置。
分析原因:
这是因为spring boot 会默认加载org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
这个类,
DataSourceAutoConfiguration类使用了@Configuration注解向spring注入了dataSource bean。因为工程中没有关于dataSource相关的配置信息,当spring创建dataSource bean因缺少相关的信息就会报错。
解决方案一:
在类定义上增加@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})的注解。
解决方案二:
在 application.properties 文件中添加数据库配置,例如:
#spring.datasource.type=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://