一、准备工作
环境:
jdk: jdk1.8
maven: maven3.39
Maven项目对象模型(POM),可以通过一小段描述信息来管理项目的构建,报告和文档的软件项目管理工具。
http://maven.apache.org/download.cgi
安装:http://jingyan.baidu.com/article/20095761bd195ecb0621b465.html
![](https://img.haomeiwen.com/i6066852/df71e821ffc4eb6a.png)
IDEA
http://www.jetbrains.com/idea/
![](https://img.haomeiwen.com/i6066852/d23ec02d5196a03a.png)
二、新建一个项目HelloSpringBoot
![](https://img.haomeiwen.com/i6066852/c5b26f1005bd5287.png)
![](https://img.haomeiwen.com/i6066852/443d0e3116212bcd.png)
![](https://img.haomeiwen.com/i6066852/b1d0345a3712cd1b.png)
![](https://img.haomeiwen.com/i6066852/d04d8117a50091c4.png)
关于Maven详解
.Maven的作用
在开发中,为了保证编译通过,我们会到处去寻找jar包,当编译通过了,运行的时候,却发现"ClassNotFoundException",我们想到的是,难道还差jar包?
每个Java项目的目录结构都没有一个统一的标准,配置文件到处都是,单元测试代码到底应该放在那里也没有一个权威的规范。
因此,我们就要用到Maven(使用Ant也可以,不过编写Ant的xml脚本比较麻烦)----一个项目管理工具。
Maven主要做了两件事:
统一开发规范与工具
统一管理jar包
![](https://img.haomeiwen.com/i6066852/a6a0eaa2ab1ee41e.png)
![](https://img.haomeiwen.com/i6066852/ff9f201f1fa89e96.png)
![](https://img.haomeiwen.com/i6066852/4a38cd08f1f40b8a.png)
第一种启动方式:
![](https://img.haomeiwen.com/i6066852/d769b27e1efce2c7.png)
真慢。。
![](https://img.haomeiwen.com/i6066852/520f5017cc752d75.png)
访问http://localhost:8080/
因为什么页面都没写,所以是返回404
![](https://img.haomeiwen.com/i6066852/95b39d21830c3e6d.png)
新建一个类HelloController
![](https://img.haomeiwen.com/i6066852/bfb49d561c55b21c.png)
![](https://img.haomeiwen.com/i6066852/6e95f2af1ba73a05.png)
@RestController
public class HelloController {
@RequestMapping(value = "/hello" , method = RequestMethod.GET)
public String say(){
return "Hello Spring Boot!";
}
}
访问http://localhost:8080/hello
![](https://img.haomeiwen.com/i6066852/1624c333811ece0a.png)
第二种启动方式
进入到项目目录
mvn spring-boot:run
![](https://img.haomeiwen.com/i6066852/f03ea5966e5220f3.png)
第三种启动方式
![](https://img.haomeiwen.com/i6066852/f01c2415063f8ba8.png)
//先编译下环境
mvn install
![](https://img.haomeiwen.com/i6066852/f7d6cdd16086c5f0.png)
cd target
java -jar test-0.0.1-SNAPSHOT.jar
![](https://img.haomeiwen.com/i6066852/fddbc071ef5dcddc.png)
![](https://img.haomeiwen.com/i6066852/6cc3d91ddab1a5db.png)
三、属性配置
application.properties
修改下端口和访问路径
server.port=8081
server.context-path=/test
![](https://img.haomeiwen.com/i6066852/72c8a4d471e1ddc6.png)
启动:
![](https://img.haomeiwen.com/i6066852/204d07ac52175cc6.png)
访问:http://localhost:8081/test/hello
![](https://img.haomeiwen.com/i6066852/5cb9a3edb0977987.png)
使用yml文件配置
yml简介
YML文件格式是YAML (YAML Aint Markup Language)编写的文件格式,YAML是一种直观的能够被电脑识别的的数据数据序列化格式,并且容易被人类阅读,容易和脚本语言交互的,可以被支持YAML库的不同的编程语言程序导入,比如: C/C++, Ruby, Python, Java, Perl, C#, PHP等。
其他比较
http://www.cnblogs.com/songchaoke/p/3376323.html
application.yml
设置端口号改为:8082,区分下。
server:
port: 8082
context-path: /test
![](https://img.haomeiwen.com/i6066852/950d6862fffdb75a.png)
删除application.properties
访问http://localhost:8082/test/hello
![](https://img.haomeiwen.com/i6066852/3d0f0f6405e6a620.png)
配置一个参数 cupSize:
![](https://img.haomeiwen.com/i6066852/76dbfff88537d47b.png)
server:
port: 8082
context-path: /test
cupSize: B
在HelloController.java 中获取到。
![](https://img.haomeiwen.com/i6066852/79f4183428e96f3f.png)
@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}"
![](https://img.haomeiwen.com/i6066852/a694c326def1762b.png)
修改代码:
![](https://img.haomeiwen.com/i6066852/6f2ae3315c19a6a4.png)
@Value("${server.cupSize}")
public String cupSize;
运行http://localhost:8082/test/hello
![](https://img.haomeiwen.com/i6066852/e16ee08039a1e3b0.png)
content
![](https://img.haomeiwen.com/i6066852/88f2770e0aba17b8.png)
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
![](https://img.haomeiwen.com/i6066852/3146e9837df1643c.png)
多项注入参数
![](https://img.haomeiwen.com/i6066852/148635f4754dccd8.png)
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();
}
}
运行结果:
![](https://img.haomeiwen.com/i6066852/726ab7ea6032c0b7.png)
多个配置文件application.yml转换
application.yml
![](https://img.haomeiwen.com/i6066852/18d719bbf9dae5f1.png)
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
![](https://img.haomeiwen.com/i6066852/9ccdaba5bc0a926f.png)
修改 :application.yml
spring:
profiles:
active: prod
访问:http://localhost:8081/test/hello
![](https://img.haomeiwen.com/i6066852/c7f730abf9ac4b86.png)
同时启动不同配置下的服务
启动application-prod.yml配置环境
//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
可以尝试启动下,注意端口不要被占用。
知识点:
![](https://img.haomeiwen.com/i6066852/949331d540d86659.png)
四、Controller的使用
![](https://img.haomeiwen.com/i6066852/bfdff228e897beb4.png)
@Controller
@ResponseBody
等同于
@RestController
分别访问"/hello","/hi" 后缀得到相同结果。
@RequestMapping(value = {"/hello","/hi"} , method = RequestMethod.GET)
访问:http://localhost:8081/test/hi
![](https://img.haomeiwen.com/i6066852/794911a20514a703.png)
@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();
}
}
![](https://img.haomeiwen.com/i6066852/f3b6107e3754a92e.png)
我们也可以看到启动项中已经标明访问路径:
![](https://img.haomeiwen.com/i6066852/a3fe6483f2fc3c3a.png)
访问:http://localhost:8081/test/hi/say
![](https://img.haomeiwen.com/i6066852/1dcbe0b0fc1af172.png)
method = RequestMethod.POST 可以下载个postman 试试
注解参数
![](https://img.haomeiwen.com/i6066852/69f698f66200f675.png)
@PathVariable
@RequestMapping(value = "/say/{id}" , method = RequestMethod.GET)
public String say(@PathVariable("id") Integer id){
return "id : " + id;
}
![](https://img.haomeiwen.com/i6066852/220627ae2c294c5c.png)
访问:http://localhost:8081/test/hi/say/11
![](https://img.haomeiwen.com/i6066852/63f1a0123ca19413.png)
访问:http://localhost:8081/test/hi/say/xxx
报错!
![](https://img.haomeiwen.com/i6066852/cafbd94cdbc7dbad.png)
同理:
@RequestMapping(value = "/{id}/say" , method = RequestMethod.GET)
前后输入效果一样的
@RequestParam
@RequestMapping(value = "/say" , method = RequestMethod.GET)
public String say(@RequestParam("id") Integer myid){
return "id : " + myid;
}
![](https://img.haomeiwen.com/i6066852/f9d414f5be2eca29.png)
访问:http://localhost:8081/test/hi/say?idd=111
![](https://img.haomeiwen.com/i6066852/e5fbc2ce25a1716f.png)
访问:http://localhost:8081/test/hi/say?idd=
![](https://img.haomeiwen.com/i6066852/e60c567c018adbd8.png)
修改代码:
@RequestMapping(value = "/say" , method = RequestMethod.GET)
public String say(@RequestParam(value="id",required = false,defaultValue = "0") Integer myid){
return "idd : " + myid;
}
![](https://img.haomeiwen.com/i6066852/b5f47f1a10ace8c9.png)
不写参数默认是 0
![](https://img.haomeiwen.com/i6066852/a68f78b957cc3096.png)
此外:(同理POST)
@RequestMapping(value = "/say" , method = RequestMethod.GET)
//可简化成:
@GetMapping(value = "/say")
网友评论