SPRING INITIALIZR是spring boot在构建项目时候非常有效, 虽然说Maven以及Spring boot提供的starter使用起来非常简单, 但是由于组件和关联部分众多,有这样一个可视化的配置构建管理工具仍然很不错. 在这篇文章中我们将会在IntelliJ IDEA中利用SPRING INITIALIZR来创建一个Hello World的例子程序, 不包括Maven下载的速度的话, 1分钟应该够了.
创建一个SPRING INITIALIZR项目
使用IntelliJ IDEA创建一个SPRING INITIALIZR类型的项目,除了选择web设定之外,一切全部Default。
选择Web 如果没有IntelliJ使用STS创建Starter类型的也是一样方便。二者都没有可以使用如下连接生成一个初始的项目zip包都是可以的。
URLhttps://start.spring.io/
项目详细
创建之后安静地等待Maven把所有的依赖全部下载下来。抽空看一下创建的内容。 可以看到,右侧代码区加亮部分正是SpringBoot的入口调用。 而左侧下方则是Maven辛辛苦苦替我们下载的内容。
Pom文件
Pom.xml文件如下所示
<?xml version="1.0" encoding="UTF-8"?>
<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>com.example
</groupId>
<artifactId>demo
</artifactId>
<version>0.0.1-SNAPSHOT
</version>
<packaging>jar
</packaging>
<name>demo
</name>
<description>Demo project for Spring Boot
</description>
<parent>
<groupId>org.springframework.boot
</groupId>
<artifactId>spring-boot-starter-parent
</artifactId>
<version>1.4.2.RELEASE
</version>
<relativePath/>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8
</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8
</project.reporting.outputEncoding>
<java.version>1.8
</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot
</groupId>
<artifactId>spring-boot-starter-web
</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot
</groupId>
<artifactId>spring-boot-starter-test
</artifactId>
<scope>test
</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot
</groupId>
<artifactId>spring-boot-maven-plugin
</artifactId>
</plugin>
</plugins>
</build>
</project>
可以在的pendency中清楚地看到我们加入的Web部分。
<dependency>
<groupId>org.springframework.boot
</groupId>
<artifactId>spring-boot-starter-web
</artifactId>
</dependency>
修改DemoApplication
加入如下两行内容
@RequestMapping(
"/")
public String
Hello(){
return "Hello, Spring Boot...";
}
同时对DemoApplication加上RestController注解,修改后的source如下
package com.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@SpringBootApplication
public class DemoApplication {
@RequestMapping(
"/")
public String
Hello(){
return "Hello, Spring Boot...";
}
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
运行
右键运行即可
结果确认
运行起来后就可以在浏览器中查看结果了
Console输出
. ____ _ __ _ _
/\\ / ___
( ( )\___ |
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1
.4.2.RELEASE)
2016-
12-
03 08:
57:
58.888 INFO
5128 --- [ main] com.example.DemoApplication : Starting DemoApplication
on vcc-PC
with PID
5128 (C:\Users\Administrator\IdeaProjects\demo\target\classes started
by Administrator
in C:\Users\Administrator\IdeaProjects\demo)
2016-
12-
03 08:
57:
58.891 INFO
5128 --- [ main] com.example.DemoApplication : No active profile
set, falling back
to default profiles:
default
2016-
12-
03 08:
57:
58.949 INFO
5128 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@
30ee2816: startup
date [Sat Dec
03 08:
57:
58 CST
2016]; root
of context hierarchy
2016-
12-
03 08:
58:
00.512 INFO
5128 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized
with port(s):
8080 (http)
2016-
12-
03 08:
58:
00.525 INFO
5128 --- [ main] o.apache.catalina.core.StandardService : Starting service Tomcat
2016-
12-
03 08:
58:
00.526 INFO
5128 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/
8.5.6
2016-
12-
03 08:
58:
00.624 INFO
5128 --- [ost-startStop-
1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2016-
12-
03 08:
58:
00.624 INFO
5128 --- [ost-startStop-
1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed
in 1678 ms
2016-
12-
03 08:
58:
00.740 INFO
5128 --- [ost-startStop-
1] o.s.b.w.servlet.ServletRegistrationBean : Mapping servlet:
2016-
12-
03 08:
58:
00.744 INFO
5128 --- [ost-startStop-
1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter:
2016-
12-
03 08:
58:
00.744 INFO
5128 --- [ost-startStop-
1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter:
2016-
12-
03 08:
58:
00.744 INFO
5128 --- [ost-startStop-
1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter:
2016-
12-
03 08:
58:
00.744 INFO
5128 --- [ost-startStop-
1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter:
2016-
12-
03 08:
58:
00.962 INFO
5128 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking
for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@
30ee2816: startup
date [Sat Dec
03 08:
57:
58 CST
2016]; root
of context hierarchy
2016-
12-
03 08:
58:
01.018 INFO
5128 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped
"{[/]}" onto
public java.lang.
String com.example.DemoApplication.Hello()
2016-
12-
03 08:
58:
01.022 INFO
5128 --- [ 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)
2016-
12-
03 08:
58:
01.022 INFO
5128 --- [ 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)
2016-
12-
03 08:
58:
01.043 INFO
5128 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler
of type [
class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-
12-
03 08:
58:
01.043 INFO
5128 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler
of type [
class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-
12-
03 08:
58:
01.071 INFO
5128 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler
of type [
class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-
12-
03 08:
58:
01.174 INFO
5128 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans
for JMX exposure
on startup
2016-
12-
03 08:
58:
01.226 INFO
5128 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started
on port(s):
8080 (http)
2016-
12-
03 08:
58:
01.230 INFO
5128 --- [ main] com.example.DemoApplication : Started DemoApplication
in 2.751 seconds (JVM running
for 3.109)
2016-
12-
03 08:
58:
06.041 INFO
5128 --- [nio-
8080-exec-
1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring FrameworkServlet
2016-
12-
03 08:
58:
06.041 INFO
5128 --- [nio-
8080-exec-
1] o.s.web.servlet.DispatcherServlet : FrameworkServlet
2016-
12-
03 08:
58:
06.052 INFO
5128 --- [nio-
8080-exec-
1] o.s.web.servlet.DispatcherServlet : FrameworkServlet
淼叔
认证博客专家
神经网络
TensorFlow
NLP
资深架构师,PMP、OCP、CSM、HPE University讲师,EXIN DevOps Professional与DevOps Master认证讲师,曾担任HPE GD China DevOps & Agile Leader,帮助企业级客户提供DevOps咨询培训以及实施指导。熟悉通信和金融领域,有超过十年金融外汇行业的架构设计、开发、维护经验,在十几年的IT从业生涯中拥有了软件开发设计领域接近全生命周期的经验和知识积累,著有企业级DevOps技术与工具实战。