Mapping
springboot1.0版本的匹配:
@RequestMapping(value = "/hello",method = RequestMethod.GET)
method = RequestMethod.GET
method = RequestMethod.POST
method = RequestMethod.PUT
method = RequestMethod.DELETE
springboot2.0版本的匹配:
@GetMapping("/hello")
@PostMapping("/hello")
@PutMapping("/hello")
@DeleteMapping("/hello")
Properties
server.port=8081
server.servlet.context-path=/luckymoney (新版本)
server.context-path=/luckmoney (旧版本)
yml
全局设置
server:
port: 8081
servlet:
context-path: /luckymoney
引用值①
minMoney: 1
maxMoney: 999
@Value("${minMoney}")
private BigDecimal minMoney;
@Value("${description}")
private String description;
引用值②
对象配置
重点:@Component、@ConfigurationProperties
limit:
minMoney: 1
maxMoney: 999
description: 最少要发${limit.minMoney}元,最多能发${limit.maxMoney}元
@Component
@ConfigurationProperties(prefix = "limit")
public class LimitConfig {
private BigDecimal minMoney;
private BigDecimal maxMoney;
private String description;
getter()/setter()...
}
@Autowired
private LimitConfig LimitConfig;
@GetMapping("/hello")
public String say(){
return "说明" + LimitConfig.getDescription();
}
开发生产分离
新建两个yml,分别为application-dev.yml(开发),application-prod.yml(生产),通过修改application.yml的spring.prfiles.active指定dev/prod
spring:
profiles:
active: prod
Controller相关注解
- @Controller 处理http请求,单独的@Controller需要模板,引入thymeleaf依赖
- @RestController spring4注解,等同于@ResponseBody+@Controller返回Json
- @RequestMapping springboot1.0配置url路径,springboot2.0有@GetMapping...
路径信息获得
@RequestParam("") 获取?后面的值,名称需要匹配
@PathValue("") 获取/后面的值
网友评论