1、spring-boot學習筆記(一)簡單入門
阿新 • • 發佈:2018-04-05
ava project nal run plugin mailto 5.4 安全 class a
一、新建普通Maven工程
pom.xml
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.4.RELEASE</version> <relativePath/> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> <finalName>SpringBoot001_HelloWorld</finalName> </build>
二、編寫代碼
@SpringBootApplication @RestController public class Application { @RequestMapping("/hello") public String hello(){ return "hello world"; } @RequestMapping("/geeting") public String geeting(){ return "Index page"; } @RequestMapping("/geeting/{user}") public String get(@PathVariable("user") String name ){ if(name.equals("zhangsan")){ return name+" Hello"; } return "hello"; } public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
**@SpringBootApplication** :指定是否為SpringBoot應用
**@RestContrller** :這個一般加在類上,說明這是一個Rest應用,每個方法返回的不再是視圖
**@RequestMapping** :url路徑映射
**@PathVariable** :讀取路徑中的變量值 賦值給name
三、代碼執行
直接先mvn package 打出jar包 在命令行中執行 java -jar jar包路徑
以下內容作為了解即可;等到世紀應用的時候看下
四、停止
1)、強制停止
ps aux | grep spring | xargs kill -9
2)、通過前臺停止
pom文件加入
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
application.yml 加入
#啟用shutdown
endpoints:
shutdown:
enabled: true
#禁用密碼驗證
sensitive: false
3)、加入密碼驗證
pom.xml加入
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
application.properties加入
#開啟shutdown的安全驗證
endpoints.shutdown.sensitive=true
#驗證用戶名
security.user.name=admin
#驗證密碼
security.user.password=admin
#角色
management.security.role=SUPERUSER
# 指定端口
management.port=8081
# 指定地址
management.address=127.0.0.1
4)關閉命令
curl -u admin:admin -X POST http://127.0.0.1:8081/manage/shutdown
# Demo
$ curl -u admin:admin -X POST http://127.0.0.1:8081/manage/shutdown
{"message":"Shutting down, bye..."}
1、spring-boot學習筆記(一)簡單入門