一、准备工作
环境:
jdk: jdk1.8
maven: maven3.39
Maven项目对象模型(POM),可以通过一小段描述信息来管理项目的构建,报告和文档的软件项目管理工具。
http://maven.apache.org/download.cgi
安装:http://jingyan.baidu.com/article/20095761bd195ecb0621b465.html

IDEA
http://www.jetbrains.com/idea/

二、新建一个项目HelloSpringBoot




关于Maven详解
.Maven的作用
在开发中,为了保证编译通过,我们会到处去寻找jar包,当编译通过了,运行的时候,却发现"ClassNotFoundException",我们想到的是,难道还差jar包?
每个Java项目的目录结构都没有一个统一的标准,配置文件到处都是,单元测试代码到底应该放在那里也没有一个权威的规范。
因此,我们就要用到Maven(使用Ant也可以,不过编写Ant的xml脚本比较麻烦)----一个项目管理工具。
Maven主要做了两件事:
统一开发规范与工具
统一管理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

第三种启动方式

//先编译下环境
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是一种直观的能够被电脑识别的的数据数据序列化格式,并且容易被人类阅读,容易和脚本语言交互的,可以被支持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

删除application.properties
访问http://localhost:8082/test/hello

配置一个参数 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

多项注入参数

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();
}
}
运行结果:

多个配置文件application.yml转换
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配置环境
//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" 后缀得到相同结果。
@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();
}
}

我们也可以看到启动项中已经标明访问路径:

访问:http://localhost:8081/test/hi/say

method = RequestMethod.POST 可以下载个postman 试试
注解参数

@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;
}

不写参数默认是 0

此外:(同理POST)
@RequestMapping(value = "/say" , method = RequestMethod.GET)
//可简化成:
@GetMapping(value = "/say")
网友评论