使用maven搭建springboot项目,有以下几种方式:
1. http://start.spring.io/
在上面网站选填项目基本信息(如:项目类型、语言、Spring Boot版本等),以及需要导入的依赖,点击“Generate Project alt + enter”生成 Spring Boot 项目的 zip 文件,下载解压导入IDE即可(选择导入maven项目)
2. 手动创建 maven 项目
“Create a simple project”勾选不勾选都可以
2.1.1勾选“Create a simple project”
填写 group id,artifact id 等信息,点击“Finish”
创建好的目录结构如上图所示,可以根据需要修改 jdk 的版本。
生成的 pom.xml 文件内容如下:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>cn.com.first</groupId> <artifactId>myfSpringBoot</artifactId> <version>0.0.1-SNAPSHOT</version> </project>
2.1.2 不勾选“Create a simple project”
"next"-->
"Finish"-->
生成的pom.xml文件内容如下:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>cn.com.second</groupId> <artifactId>mysSpringBoot</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>mysSpringBoot</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> </project>
2.2 修改 pom.xml 文件,引入 <parent> 元素
继承<parent>pom里面已经引入了 tomcat、jetty 等很多常用依赖
2.2.1引入“1.4.0.RELEASE”版本的<parent> 元素
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>cn.com.first</groupId> <artifactId>myfSpringBoot</artifactId> <version>0.0.1-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.0.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> </project>
没有添加任何依赖也可以build。在项目根目录打开cmd命令行执行: mvn package 对项目进行打包。
会自动下载一堆 http://maven.aliyun.com 的 jar 到 maven 仓库,这是因为我的 setting.xml 文件配置的远程仓库是 aliyun 的远程仓库。(如下图)
项目构建成功后,我们来看一下项目 target 目录下发生的变化:
2.2.2引入“2.0.0.M3”版本的<parent> 元素
此时 pom.xml 文件会报一个 build error 的错,配置spring-milestones这个仓库即可
<repositories> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </pluginRepository> </pluginRepositories>
<repositories>是 jar 包库,<pluginRepositories>是插件库。
2.3 启动类(这时只是一个普通的 maven 项目)
package cn.com.second; public class StartApplication{ public static void main(String[] args){ System.out.println( "Hello Spring Boot!" ); } }
启动StartApplication
2.4 启动 springboot 项目
package cn.com.second; import org.springframework.boot.SpringApplication; public class StartApplication{ public static void main(String[] args){ SpringApplication.run(StartApplication.class, args);//需要引入下面依赖 } }
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
2.4.1 添加 springboot 默认配置文件 application.properties
2.4.2. 然后启动项目仍然不会报错,现在 controller 里面添加一个 test 方法,如下图:
此时启动类上需要添加注解 @SpringBootApplication , 否则项目启动时会报错
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.0.0.M3) 2018-06-15 14:55:11.159 INFO 15640 --- [ main] c.c.s.mysSpringBoot.StartApplication : Starting StartApplication on lkk-PC with PID 15640 (F:\workspace\mysSpringBoot\target\classes started by lkk in F:\workspace\mysSpringBoot) 2018-06-15 14:55:11.162 INFO 15640 --- [ main] c.c.s.mysSpringBoot.StartApplication : No active profile set, falling back to default profiles: default 2018-06-15 14:55:11.176 INFO 15640 --- [ main] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@527e5409: startup date [Fri Jun 15 14:55:11 CST 2018]; root of context hierarchy 2018-06-15 14:55:11.233 WARN 15640 --- [ main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: 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. 2018-06-15 14:55:11.238 WARN 15640 --- [ main] o.s.b.c.e.EventPublishingRunListener : Error calling ApplicationEventListener java.lang.ClassCastException: org.springframework.boot.context.event.ApplicationFailedEvent cannot be cast to org.springframework.boot.web.context.WebServerInitializedEvent at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:159) [spring-context-5.0.0.RC3.jar:5.0.0.RC3] at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139) [spring-context-5.0.0.RC3.jar:5.0.0.RC3] at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:127) [spring-context-5.0.0.RC3.jar:5.0.0.RC3] at org.springframework.boot.context.event.EventPublishingRunListener.finished(EventPublishingRunListener.java:114) [spring-boot-2.0.0.M3.jar:2.0.0.M3] at org.springframework.boot.SpringApplicationRunListeners.callFinishedListener(SpringApplicationRunListeners.java:79) [spring-boot-2.0.0.M3.jar:2.0.0.M3] at org.springframework.boot.SpringApplicationRunListeners.finished(SpringApplicationRunListeners.java:72) [spring-boot-2.0.0.M3.jar:2.0.0.M3] at org.springframework.boot.SpringApplication.handleRunFailure(SpringApplication.java:803) [spring-boot-2.0.0.M3.jar:2.0.0.M3] at org.springframework.boot.SpringApplication.run(SpringApplication.java:338) [spring-boot-2.0.0.M3.jar:2.0.0.M3] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1245) [spring-boot-2.0.0.M3.jar:2.0.0.M3] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1233) [spring-boot-2.0.0.M3.jar:2.0.0.M3] at cn.com.second.mysSpringBoot.StartApplication.main(StartApplication.java:9) [classes/:na] 2018-06-15 14:55:11.240 ERROR 15640 --- [ main] o.s.boot.SpringApplication : Application startup failed 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:137) ~[spring-boot-2.0.0.M3.jar:2.0.0.M3] at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543) ~[spring-context-5.0.0.RC3.jar:5.0.0.RC3] at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:122) ~[spring-boot-2.0.0.M3.jar:2.0.0.M3] at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:750) [spring-boot-2.0.0.M3.jar:2.0.0.M3] at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:386) [spring-boot-2.0.0.M3.jar:2.0.0.M3] at org.springframework.boot.SpringApplication.run(SpringApplication.java:327) [spring-boot-2.0.0.M3.jar:2.0.0.M3] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1245) [spring-boot-2.0.0.M3.jar:2.0.0.M3] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1233) [spring-boot-2.0.0.M3.jar:2.0.0.M3] at cn.com.second.mysSpringBoot.StartApplication.main(StartApplication.java:9) [classes/:na] Caused by: org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean. at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.getWebServerFactory(ServletWebServerApplicationContext.java:186) ~[spring-boot-2.0.0.M3.jar:2.0.0.M3] at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.createWebServer(ServletWebServerApplicationContext.java:160) ~[spring-boot-2.0.0.M3.jar:2.0.0.M3] at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:134) ~[spring-boot-2.0.0.M3.jar:2.0.0.M3] ... 8 common frames omittedt
同时需要引入 web 依赖:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
顺利启动:
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.0.0.M3) 2018-06-15 15:06:10.509 INFO 14536 --- [ main] c.c.s.mysSpringBoot.StartApplication : Starting StartApplication on lkk-PC with PID 14536 (F:\workspace\mysSpringBoot\target\classes started by lkk in F:\workspace\mysSpringBoot) 2018-06-15 15:06:10.512 INFO 14536 --- [ main] c.c.s.mysSpringBoot.StartApplication : No active profile set, falling back to default profiles: default 2018-06-15 15:06:10.550 INFO 14536 --- [ main] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@527e5409: startup date [Fri Jun 15 15:06:10 CST 2018]; root of context hierarchy 2018-06-15 15:06:11.655 INFO 14536 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8082 (http) 2018-06-15 15:06:11.667 INFO 14536 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2018-06-15 15:06:11.668 INFO 14536 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.16 2018-06-15 15:06:11.767 INFO 14536 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2018-06-15 15:06:11.768 INFO 14536 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1220 ms 2018-06-15 15:06:11.937 INFO 14536 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/] 2018-06-15 15:06:11.941 INFO 14536 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'metricsFilter' to: [/*] 2018-06-15 15:06:11.941 INFO 14536 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*] 2018-06-15 15:06:11.942 INFO 14536 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*] 2018-06-15 15:06:11.942 INFO 14536 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*] 2018-06-15 15:06:11.942 INFO 14536 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*] 2018-06-15 15:06:11.942 INFO 14536 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'webRequestLoggingFilter' to: [/*] 2018-06-15 15:06:12.289 INFO 14536 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@527e5409: startup date [Fri Jun 15 15:06:10 CST 2018]; root of context hierarchy 2018-06-15 15:06:12.357 INFO 14536 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/test]}" onto public java.lang.String cn.com.second.mysSpringBoot.controller.SecondController.test() 2018-06-15 15:06:12.364 INFO 14536 --- [ 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.servlet.error.BasicErrorController.error(javax.servlet.http.HttpServletRequest) 2018-06-15 15:06:12.364 INFO 14536 --- [ 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.servlet.error.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse) 2018-06-15 15:06:12.391 INFO 14536 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2018-06-15 15:06:12.391 INFO 14536 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2018-06-15 15:06:12.426 INFO 14536 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2018-06-15 15:06:12.628 INFO 14536 --- [ main] o.s.b.a.e.mvc.EndpointHandlerMapping : Mapped "{[/application/beans || /application/beans.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v2+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke() 2018-06-15 15:06:12.630 INFO 14536 --- [ main] o.s.b.a.e.mvc.EndpointHandlerMapping : Mapped "{[/application/loggers/{name:.*}],methods=[GET],produces=[application/vnd.spring-boot.actuator.v2+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.LoggersMvcEndpoint.get(java.lang.String) 2018-06-15 15:06:12.631 INFO 14536 --- [ main] o.s.b.a.e.mvc.EndpointHandlerMapping : Mapped "{[/application/loggers/{name:.*}],methods=[POST],consumes=[application/vnd.spring-boot.actuator.v2+json || application/json],produces=[application/vnd.spring-boot.actuator.v2+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.LoggersMvcEndpoint.set(java.lang.String,java.util.Map<java.lang.String, java.lang.String>) 2018-06-15 15:06:12.631 INFO 14536 --- [ main] o.s.b.a.e.mvc.EndpointHandlerMapping : Mapped "{[/application/loggers || /application/loggers.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v2+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke() 2018-06-15 15:06:12.632 INFO 14536 --- [ main] o.s.b.a.e.mvc.EndpointHandlerMapping : Mapped "{[/application/metrics/{name:.*}],methods=[GET],produces=[application/vnd.spring-boot.actuator.v2+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.MetricsMvcEndpoint.value(java.lang.String) 2018-06-15 15:06:12.632 INFO 14536 --- [ main] o.s.b.a.e.mvc.EndpointHandlerMapping : Mapped "{[/application/metrics || /application/metrics.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v2+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke() 2018-06-15 15:06:12.633 INFO 14536 --- [ main] o.s.b.a.e.mvc.EndpointHandlerMapping : Mapped "{[/application/auditevents || /application/auditevents.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v2+json || application/json]}" onto public org.springframework.http.ResponseEntity<?> org.springframework.boot.actuate.endpoint.mvc.AuditEventsMvcEndpoint.findByPrincipalAndAfterAndType(java.lang.String,java.util.Date,java.lang.String) 2018-06-15 15:06:12.634 INFO 14536 --- [ main] o.s.b.a.e.mvc.EndpointHandlerMapping : Mapped "{[/application/env/{name:.*}],methods=[GET],produces=[application/vnd.spring-boot.actuator.v2+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EnvironmentMvcEndpoint.value(java.lang.String) 2018-06-15 15:06:12.634 INFO 14536 --- [ main] o.s.b.a.e.mvc.EndpointHandlerMapping : Mapped "{[/application/env || /application/env.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v2+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke() 2018-06-15 15:06:12.635 INFO 14536 --- [ main] o.s.b.a.e.mvc.EndpointHandlerMapping : Mapped "{[/application/autoconfig || /application/autoconfig.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v2+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke() 2018-06-15 15:06:12.636 INFO 14536 --- [ main] o.s.b.a.e.mvc.EndpointHandlerMapping : Mapped "{[/application/trace || /application/trace.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v2+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke() 2018-06-15 15:06:12.637 INFO 14536 --- [ main] o.s.b.a.e.mvc.EndpointHandlerMapping : Mapped "{[/application/health || /application/health.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v2+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.HealthMvcEndpoint.invoke(javax.servlet.http.HttpServletRequest,java.security.Principal) 2018-06-15 15:06:12.637 INFO 14536 --- [ main] o.s.b.a.e.mvc.EndpointHandlerMapping : Mapped "{[/application/info || /application/info.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v2+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke() 2018-06-15 15:06:12.638 INFO 14536 --- [ main] o.s.b.a.e.mvc.EndpointHandlerMapping : Mapped "{[/application/mappings || /application/mappings.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v2+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke() 2018-06-15 15:06:12.638 INFO 14536 --- [ main] o.s.b.a.e.mvc.EndpointHandlerMapping : Mapped "{[/application/dump || /application/dump.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v2+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke() 2018-06-15 15:06:12.639 INFO 14536 --- [ main] o.s.b.a.e.mvc.EndpointHandlerMapping : Mapped "{[/application/configprops || /application/configprops.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v2+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke() 2018-06-15 15:06:12.639 INFO 14536 --- [ main] o.s.b.a.e.mvc.EndpointHandlerMapping : Mapped "{[/application/heapdump || /application/heapdump.json],methods=[GET],produces=[application/octet-stream]}" onto public void org.springframework.boot.actuate.endpoint.mvc.HeapdumpMvcEndpoint.invoke(boolean,javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse) throws java.io.IOException,javax.servlet.ServletException 2018-06-15 15:06:12.722 INFO 14536 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup 2018-06-15 15:06:12.731 INFO 14536 --- [ main] o.s.c.support.DefaultLifecycleProcessor : Starting beans in phase 0 2018-06-15 15:06:12.818 INFO 14536 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8082 (http) 2018-06-15 15:06:12.822 INFO 14536 --- [ main] c.c.s.mysSpringBoot.StartApplication : Started StartApplication in 2.563 seconds (JVM running for 2.82)
3. @ResponseBody注解
访问2.4.2 中的test方法会404,此时需要在方法上加@ResponseBody注解