备注:
- spring-boot的版本为2.0.1.RELEASE
- 依赖redis服务;请先安装redis,接着修改application-${env}.properties中的redis配置
- 依赖mysql;运行之前,请先执行 init_script.sql,接着修改数据库配置项
- http://start.spring.io/
- IntelliJ IDEA -> New -> Project -> Spring Initializr
- Spring Boot CLI
package com.example.helloworld;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloWorldController {
@RequestMapping("/")
public String sayHello() {
return "Hello,World!";
}
}
- IntelliJ IDEA右键Run
- mvn spring-boot:run
web
mybatis
redis
freemarker
...
- 命令行参数
- java:comp/env里的JNDI属性
- JVM系统属性
- 操作系统环境变量
- 随机生成的带random.*前缀的属性(在设置其他属性时,可以引用它们,比如
${random.long}
) - 应用程序以外的application.properties或者appliaction.yml文件
- 打包在应用程序内的application.properties或者appliaction.yml文件
- 通过@PropertySource标注的属性源
- 默认属性
application.properties中配置spring.profiles.active=${env}
- dev
- test
- pre
- prod
- 开启Actuator
#打开actuator
management.endpoints.enabled-by-default=true
management.endpoints.web.exposure.include=*
management.endpoints.web.base-path=/actuator
management.endpoint.beans.enabled=true
management.endpoint.env.enabled=true
management.endpoint.health.enabled=true
management.endpoint.httptrace.enabled=true
management.endpoint.info.enabled=true
management.endpoint.metrics.enabled=true
- 请求
- spring-security使用演示
- 自定义CustomAuthenticationProvider,增加登录失败次数限制
- 禁用csrf
- HttpSecurity.csrf().disable()
- 默认打开csrf
- login页面需要传递
${_csrf.parameterName}=${_csrf.token}
,要不然页面403报错 - logout默认不支持GET请求方式,会发生404报错
- logout可以添加
logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
而使GET请求方式得以支持
- login页面需要传递
- mybatis注解
- redis限流处理
- 分钟限流
- freemarker
- error页面
- Json
可以使用的方式
- 继承BasicErrorController;实现ErrorController接口
- RestControllerAdvice + ExceptionHandler
- ControllerAdvice + ExceptionHandler
- 实现ErrorViewResolver接口
- HandlerExceptionResolver
注意事项
- 区分请求头信息
- 默认浏览器打开,如果请求头部未传递
Content-Type: application/json
,即使restful接口是返回json数据,但发生异常的时候,走的是错误页面处理,而不是json的统一异常处理 - 请求Header中的Content-Type设置为application/json,则发生异常的时候,会走json统一处理
- https://spring.io/guides
- https://docs.spring.io/spring-boot/docs/current/reference/html/
- https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html
- https://segmentfault.com/a/1190000008893479
- https://blog.csdn.net/t894690230/article/details/52404105
- https://stackoverflow.com/questions/23187109/spring-security-404-on-logout
- https://docs.spring.io/spring-security/site/docs/current/reference/htmlsingle/#csrf-logout
- https://juejin.im/post/5a6c2333f265da3e3d496125
- http://www.mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/
- https://www.cnblogs.com/waterystone/p/6214212.html
- http://codippa.com/ambiguous-exceptionhandler-method-mapped-error-in-spring/