Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。通过这种方式,Spring Boot致力于在蓬勃发展的快速应用开发领域(rapid application development)成为领导者。(From 百度百科)
1、新建第一个SpringBoot Web工程
在IDEA中,新建工程时选择Spring Initializr
:
修改Group名和Artifact名称:
修改Group名和Artifact名称
依赖选择Web:
依赖选择Web
输入工程名称:
输入工程名称
创建完成后的工程目录结构如下:
工程目录结构
Spring Boot的基础结构共三个文件(具体路径根据用户生成项目时填写的Group和Artifact所有差异):
- src/main/java下的程序入口:
com.lfqy.HelloworldApplication.java
- src/main/resources下的配置文件:
application.properties
,当前该文件内容为空。 - src/test/下的测试入口:
com.lfqy.HelloworldApplication.java
2、实现第一个helloworld
这里大概分几步:新建url的响应逻辑;设置basePackage;更新maven。
2.1 新建url的响应逻辑
在src/main/java
目录下,新建一个包com.hello.world.controller
。
然后,在该包中新建一个类HelloWorldController.java
,代码如下:
package com.hello.world.controller;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Created by chengxia on 2019/3/25.
*/
@RestController
@EnableAutoConfiguration
public class HelloWorldController {
@RequestMapping
public String index() {
return "Hello World";
}
@RequestMapping("/helloworld")
public String helloworld() {
return "Hello World";
}
@RequestMapping("test/hello")
public String testHelloworld() {
return "Hello World";
}
@RequestMapping("/info")
public Map<String,String> getInfo(@RequestParam String name){
Map<String,String> map = new HashMap<>();
map.put ("name",name);
return map;
}
@RequestMapping("/list")
public List<Map<String,String>> getList(){
List<Map<String,String>> list = new ArrayList<>();
Map<String,String> map = null;
for (int i=1;i<=5;i++){
map = new HashMap<> ();
map.put ("name","gogogo"+i);
list.add (map);
}
return list;
}
}
2.2 设置basePackage
这是很关键的一步,不然工程可以启动,但是访问url会报404
。打开工程中的HelloworldApplication.java
文件,在其中给这个类加一个注解@ComponentScan(basePackages = "com.hello.world.controller")
。修改后的HelloworldApplication.java
文件如下:
package com.lfqy;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan(basePackages = "com.hello.world.controller")
public class HelloworldApplication {
public static void main(String[] args) {
SpringApplication.run(HelloworldApplication.class, args);
}
}
2.3 更新maven
这一步对于新手来说,比较容易忽略,现象就是上面做完之后,打开HelloworldApplication.java,找不到运行的按钮。
在IDEA中,点开MAVEN标签,点击刷新按钮,如下图:
执行maven刷新
更新之后,会发现源文件的图标都变了(如上图)。
3、运行
打开HelloworldApplication.java,在该源码文件中,右键鼠标,可以看到名为Run'HelloworldAppl....main()'
的运行按钮,点击就启动了工程。控制台输出如下:
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.1.3.RELEASE)
2019-03-25 09:58:15.951 INFO 694 --- [ main] com.lfqy.HelloworldApplication : Starting HelloworldApplication on ChengdeMacBook-Pro.local with PID 694 (/Users/chengxia/Developer/Java/springboothelloworld/target/classes started by chengxia in /Users/chengxia/Developer/Java/springboothelloworld)
2019-03-25 09:58:15.955 INFO 694 --- [ main] com.lfqy.HelloworldApplication : No active profile set, falling back to default profiles: default
2019-03-25 09:58:17.381 INFO 694 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2019-03-25 09:58:17.425 INFO 694 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2019-03-25 09:58:17.426 INFO 694 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.16]
2019-03-25 09:58:17.440 INFO 694 --- [ main] o.a.catalina.core.AprLifecycleListener : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [/Users/chengxia/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:.]
2019-03-25 09:58:17.593 INFO 694 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2019-03-25 09:58:17.594 INFO 694 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1531 ms
2019-03-25 09:58:17.948 INFO 694 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2019-03-25 09:58:18.282 INFO 694 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2019-03-25 09:58:18.287 INFO 694 --- [ main] com.lfqy.HelloworldApplication : Started HelloworldApplication in 18.044 seconds (JVM running for 18.886)
在浏览器中访问如下地址:
http://127.0.0.1:8080/
http://127.0.0.1:8080/helloworld
http://127.0.0.1:8080/test/hello
http://127.0.0.1:8080/list
http://127.0.0.1:8080/info?name=Paopao
就可得到我们前面代码中的逻辑响应。下图是一个例子:
网友评论