文章目录
代码见
https://gitee.com/gaoxinfu/demo-spring-boot/tree/master/%20demo-spring-boot-example-01
1.设置spring-boot的parent
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
</parent>
说明:
1.Spring boot的项目必须要将parent设置为spring boot的parent,
该parent包含了大量默认的配置,大大简化了我们的开发。
2.各位同学可以点击pom文件中上面的链接,看一下都默认配置了哪些东西;
2.导入spring boot的web支持
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!-- <version></version> 由于我们在上面指定了 parent(spring boot) -->
</dependency>
会发现 spring-boot-starter-web: MVC,AOP的依赖包都在这里有了
主要web开发
3.添加Spring boot的插件
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
3.1.Spring Boot Maven plugin 作用
1.Spring Boot的Maven插件(Spring Boot Maven plugin)能够以Maven的方式为应用
提供Spring Boot的支持,即为Spring Boot应用提供了执行Maven操作的可能。
2.Spring Boot Maven plugin能够将Spring Boot应用打包为可执行的jar或war文件,
然后以通常的方式运行Spring Boot应用。
3.Spring Boot Maven plugin的最新版本为2017.6.8发布的1.5.4.RELEASE,要求Java 8, Maven 3.2及以后。
3.2.Spring Boot Maven plugin的5个Goals
1.spring-boot:repackage,默认goal。在mvn package之后,再次打包可执行的jar/war,
同时保留mvn package生成的2.jar/war为.origin
3.spring-boot:run,运行Spring Boot应用
4.spring-boot:start,在mvn integration-test阶段,进行Spring Boot应用生命周期的管理
5.spring-boot:stop,在mvn integration-test阶段,进行Spring Boot应用生命周期的管理
6.spring-boot:build-info,生成Actuator使用的构建信息文件build-info.properties
4.编写第一个spring-boot程序
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
*
* @Description:
* @RestController 的意思就是controller里面的方法都以json格式输出,不用再写什么jackjson配置的了!
* @Author:gaoxinfu
* @Time:2018年4月18日 上午10:56:04
*/
@RestController
public class HelloWorldController {
/**
*
* @Title: index
* @Description:
* 启动主程序,打开浏览器访问
* http://localhost:8080/hello
* ,就可以看到效果了,有木有很简单!
* @return
* @return: String
* @throws
* @Exception:
* @Author: gaoxinfu
* @Time:2018年4月18日 上午11:15:43
*/
@RequestMapping("/hello")
public String index() {
return "Hello World";
}
}
备注
1、@SpringBootApplication:Spring Boot项目的核心注解,主要目的是开启自动配置。;
2、@Configuration:这是一个配置Spring的配置类;
3、@Controller:标明这是一个SpringMVC的Controller控制器;
4、main方法:在main方法中启动一个应用,即:这个应用的入口;
5.启动方式
直接运行main方法即可
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.5.2.RELEASE)
2019-01-06 16:18:21.163 INFO 5144 --- [ main] c.g.demo.springboot.HelloApplication : Starting HelloApplication on gaoxinfu with PID 5144 (E:\workspace\eclipse\luna\demo-spring-boot-example-01\target\classes started by www.0001.GA in E:\workspace\eclipse\luna\demo-spring-boot-example-01)
2019-01-06 16:18:21.169 INFO 5144 --- [ main] c.g.demo.springboot.HelloApplication : No active profile set, falling back to default profiles: default
2019-01-06 16:18:21.277 INFO 5144 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@223877f8: startup date [Sun Jan 06 16:18:21 CST 2019]; root of context hierarchy
2019-01-06 16:18:23.577 INFO 5144 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2019-01-06 16:18:23.591 INFO 5144 --- [ main] o.apache.catalina.core.StandardService : Starting service Tomcat
2019-01-06 16:18:23.592 INFO 5144 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.11
2019-01-06 16:18:23.710 INFO 5144 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2019-01-06 16:18:23.710 INFO 5144 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 2437 ms
2019-01-06 16:18:23.936 INFO 5144 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]
2019-01-06 16:18:23.942 INFO 5144 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2019-01-06 16:18:23.942 INFO 5144 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2019-01-06 16:18:23.943 INFO 5144 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2019-01-06 16:18:23.943 INFO 5144 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
2019-01-06 16:18:24.355 INFO 5144 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@223877f8: startup date [Sun Jan 06 16:18:21 CST 2019]; root of context hierarchy
2019-01-06 16:18:24.445 INFO 5144 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/hello]}" onto public java.lang.String com.gaoxinfu.demo.springboot.HelloApplication.hello()
2019-01-06 16:18:24.453 INFO 5144 --- [ 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)
2019-01-06 16:18:24.454 INFO 5144 --- [ 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)
2019-01-06 16:18:24.504 INFO 5144 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2019-01-06 16:18:24.505 INFO 5144 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2019-01-06 16:18:24.574 INFO 5144 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2019-01-06 16:18:24.901 INFO 5144 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2019-01-06 16:18:24.983 INFO 5144 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2019-01-06 16:18:25.003 INFO 5144 --- [ main] c.g.demo.springboot.HelloApplication : Started HelloApplication in 4.303 seconds (JVM running for 4.661)
5.验证
http://localhost:8080/hello