一贬堵、準備工作
環(huán)境:
jdk: jdk1.8
maven: maven3.39
Maven項目對象模型(POM),可以通過一小段描述信息來管理項目的構(gòu)建蒸殿,報告和文檔的軟件項目管理工具。
http://maven.apache.org/download.cgi
安裝:http://jingyan.baidu.com/article/20095761bd195ecb0621b465.html
IDEA
http://www.jetbrains.com/idea/
二、新建一個項目HelloSpringBoot
關于Maven詳解
.Maven的作用
在開發(fā)中,為了保證編譯通過惰爬,我們會到處去尋找jar包,當編譯通過了风范,運行的時候硼婿,卻發(fā)現(xiàn)"ClassNotFoundException",我們想到的是州胳,難道還差jar包?
每個Java項目的目錄結(jié)構(gòu)都沒有一個統(tǒng)一的標準瓤湘,配置文件到處都是弛说,單元測試代碼到底應該放在那里也沒有一個權威的規(guī)范。
因此虎囚,我們就要用到Maven(使用Ant也可以圃伶,不過編寫Ant的xml腳本比較麻煩)----一個項目管理工具。
Maven主要做了兩件事:
統(tǒng)一開發(fā)規(guī)范與工具
統(tǒng)一管理jar包
第一種啟動方式:
真慢榔至。铅鲤。
訪問http://localhost:8080/
因為什么頁面都沒寫,所以是返回404
新建一個類HelloController
@RestController
public class HelloController {
@RequestMapping(value = "/hello" , method = RequestMethod.GET)
public String say(){
return "Hello Spring Boot!";
}
}
訪問http://localhost:8080/hello
第二種啟動方式
進入到項目目錄
mvn spring-boot:run
第三種啟動方式
//先編譯下環(huán)境
mvn install
cd target
java -jar test-0.0.1-SNAPSHOT.jar
三、屬性配置
application.properties
修改下端口和訪問路徑
server.port=8081
server.context-path=/test
啟動:
訪問:http://localhost:8081/test/hello
使用yml文件配置
yml簡介
YML文件格式是YAML (YAML Aint Markup Language)編寫的文件格式座掘,YAML是一種直觀的能夠被電腦識別的的數(shù)據(jù)數(shù)據(jù)序列化格式溢陪,并且容易被人類閱讀杉编,容易和腳本語言交互的邓馒,可以被支持YAML庫的不同的編程語言程序?qū)耄热纾?C/C++, Ruby, Python, Java, Perl, C#, PHP等救军。
其他比較
http://www.cnblogs.com/songchaoke/p/3376323.html
application.yml
設置端口號改為:8082戳寸,區(qū)分下疫鹊。
server:
port: 8082
context-path: /test
刪除application.properties
訪問http://localhost:8082/test/hello
配置一個參數(shù) cupSize:
server:
port: 8082
context-path: /test
cupSize: B
在HelloController.java 中獲取到蚌吸。
@RestController
public class HelloController {
@Value("${cupSize}")
private String cupSize;
@RequestMapping(value = "/hello" , method = RequestMethod.GET)
public String say(){
return cupSize;
}
}
報錯:
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'cupSize' in value "${cupSize}"
修改代碼:
@Value("${server.cupSize}")
public String cupSize;
運行http://localhost:8082/test/hello
content
server:
port: 8082
context-path: /test
cupSize: B
age: 18
content: "cupSize: ${server.cupSize},age: ${server.age}"
@RestController
public class HelloController {
@Value("${server.cupSize}")
public String cupSize;
@Value("${server.age}")
public Integer age;
@Value("${server.content}")
public String content;
@RequestMapping(value = "/hello" , method = RequestMethod.GET)
public String say(){
return content;
}
}
訪問http://localhost:8082/test/hello
多項注入?yún)?shù)
application.yml
server:
port: 8082
context-path: /test
girl:
cupSize: B
age: 18
新建配置類 GirlProperties.java
@Component
@ConfigurationProperties(prefix = "girl")
public class GirlProperties {
private String cupSize;
private Integer age;
public void setCupSize(String cupSize) {
this.cupSize = cupSize;
}
public void setAge(Integer age) {
this.age = age;
}
public String getCupSize() {
return cupSize;
}
public Integer getAge() {
return age;
}
}
ps:
簡化配置文件的讀取:
@value
[http://www.cnblogs.com/BensonHe/p/3963940.html]
分組獲裙惹场:
@Component作用
http://www.cnblogs.com/savage-ce/p/5667596.html
@ConfigurationProperties
@Autowired
HelloController.java
@RestController
public class HelloController {
/* @Value("${server.cupSize}")
public String cupSize;
@Value("${server.age}")
public Integer age;
@Value("${server.content}")
public String content;
*/
@Autowired
private GirlProperties girlProperties;
@RequestMapping(value = "/hello" , method = RequestMethod.GET)
public String say(){
return girlProperties.getCupSize();
}
}
運行結(jié)果:
多個配置文件application.yml轉(zhuǎn)換
application.yml
spring:
profiles:
active: dev
application-dev.yml
server:
port: 8082
context-path: /test
girl:
cupSize: B
age: 18
application-prod.yml
server:
port: 8081
context-path: /test
girl:
cupSize: F
age: 18
運行:http://localhost:8082/test/hello
修改 :application.yml
spring:
profiles:
active: prod
訪問:http://localhost:8081/test/hello
同時啟動不同配置下的服務
啟動application-prod.yml配置環(huán)境
//cd test項目目錄下
mvn install
java -jar target/test-0.0.1-SNAPSHOT.jar --spring.profiles.active=prod
java -jar target/test-0.0.1-SNAPSHOT.jar --spring.profiles.active=dev
可以嘗試啟動下墩邀,注意端口不要被占用。
知識點:
四竹海、Controller的使用
@Controller
@ResponseBody
等同于
@RestController
分別訪問"/hello","/hi" 后綴得到相同結(jié)果。
@RequestMapping(value = {"/hello","/hi"} , method = RequestMethod.GET)
訪問:http://localhost:8081/test/hi
@RequestMapping("/hi")
@Controller
@ResponseBody
@RequestMapping("/hi")
public class HelloController {
@Autowired
private GirlProperties girlProperties;
@RequestMapping(value = "/say" , method = RequestMethod.GET)
public String say(){
return girlProperties.getCupSize();
}
}
我們也可以看到啟動項中已經(jīng)標明訪問路徑:
訪問:http://localhost:8081/test/hi/say
method = RequestMethod.POST 可以下載個postman 試試
注解參數(shù)
@PathVariable
@RequestMapping(value = "/say/{id}" , method = RequestMethod.GET)
public String say(@PathVariable("id") Integer id){
return "id : " + id;
}
訪問:http://localhost:8081/test/hi/say/11
訪問:http://localhost:8081/test/hi/say/xxx
報錯园细!
同理:
@RequestMapping(value = "/{id}/say" , method = RequestMethod.GET)
前后輸入效果一樣的
@RequestParam
@RequestMapping(value = "/say" , method = RequestMethod.GET)
public String say(@RequestParam("id") Integer myid){
return "id : " + myid;
}
訪問:http://localhost:8081/test/hi/say?idd=111
訪問:http://localhost:8081/test/hi/say?idd=
修改代碼:
@RequestMapping(value = "/say" , method = RequestMethod.GET)
public String say(@RequestParam(value="id",required = false,defaultValue = "0") Integer myid){
return "idd : " + myid;
}
不寫參數(shù)默認是 0
此外:(同理POST)
@RequestMapping(value = "/say" , method = RequestMethod.GET)
//可簡化成:
@GetMapping(value = "/say")