# 一些功能解决
| maven 打包 spring boot 生成docker 镜像 | 进入:跨域问题 |
|---|---|
| api文档——Swagger | 集成kaptcha验证码插件 |
| api文档——JApiDocs | 独有得拦截器Advice |
| 初始化资源的方式 | 开启端点相关 |
| 观察者模式,抛弃for循环 |
# 编写自己的SpringBoot-Starter
starter的好处是,集成众多依赖,提供一个一站式的依赖项。 Starter相当于模块,它能将模块所需的依赖整合起来并对模块内的Bean根据环境( 条件)进行自动配置。 使用者只需要依赖相应功能的Starter,无需做过多的配置和依赖, Spring Boot就能自动扫描并加载相应的模块。
# 编写自己的SpringBoot-Starter开发步骤
- 1.新建Maven项目,在项目的POM文件中定义使用的依赖;
- 2.新建配置类,写好配置项和默认的配置值,指明配置项前缀;
- 3.新建自动装配类,使用
@Configuration和@Bean来进行自动装配; - 4.新建spring.factories文件,指定Starter的自动装配类;
# 编写自己的SpringBoot-Starter具体步骤
增加配置
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
2
3
4
5
主要的作用是在编译时在
META-INF下生成spring-configuration-metadata.json文件,该文件主要为IDE使用。 即可以通过在application.properties文件中通过ctrl + 点击进入配置属性所在的类中
# 编写自己的SpringBoot-Starter配置类
# 全局处理
# 全局异常处理
为了对自己的接口负责,我们需要进行全局的异常处理,目的是防止出现约定之外的数据结构。
# SpringBoot默认的异常处理机制
Spring Boot 会返回两种类型的异常,一种是 HTML,还有一种是 Json 格式的数据,这主要取决于请求头中的 Accept 参数,比如浏览器发出的请求,请求头中会附带 Accept:text/html,所以此时 Spring Boot 会返回一个错误页面,称为 Whitelabel Error Page,而当我们使用 Postman 请求时,返回的则是 Json 类型的数据。
原理其实也很简单,Spring Boot 默认提供了程序出错的结果映射路径 /error。而这个 /error 请求会由 BasicErrorController 来处理,其内部其实就是通过判断请求头的 Accept 中的内容来进行区分处理逻辑的(判断是否包含 text/html),从而来决定返回页面视图还是 JSON 消息内容。
org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController
@RequestMapping(produces = MediaType.TEXT_HTML_VALUE)
public ModelAndView errorHtml(HttpServletRequest request, HttpServletResponse response) {
HttpStatus status = getStatus(request);
Map<String, Object> model = Collections
.unmodifiableMap(getErrorAttributes(request, isIncludeStackTrace(request, MediaType.TEXT_HTML)));
response.setStatus(status.value());
ModelAndView modelAndView = resolveErrorView(request, response, status, model);
return (modelAndView != null) ? modelAndView : new ModelAndView("error", model);
}
@RequestMapping
public ResponseEntity<Map<String, Object>> error(HttpServletRequest request) {
Map<String, Object> body = getErrorAttributes(request, isIncludeStackTrace(request, MediaType.ALL));
HttpStatus status = getStatus(request);
return new ResponseEntity<>(body, status);
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 自定义错误页面
自定义错误页面的好处有好多,比如 404 错误页面,我们完全可以自定义 404 的 HTML 页面,上面可以放置图片等,这样体验就更友好一点。
自定义的错误页面有两种,一种是
静态页面,一种是使用模板引擎动态生成,后者的优势是可以在页面上显示自定义的内容。
- 静态页面的方式,html 文件的路径为:
resources/public/error/xxx.html
如果要替换 404 错误页面,则在此路径下放置 404.html 文件,同理,如果要替换 500 错误页面,则在此路径下放置 500.html 文件即可
- 模板引擎渲染的动态页面的方式,html 文件的路径为:
resources/templates/error/xxx.html
文件命名同上
注意:动态页面的优先级是要高于静态页面的.
比如你同时配置了静态页面和动态页面,那么最终生效的,会是动态页面。
# 自定义错误信息
上面介绍了最简单的错误处理,最主要的针对返回的 HTML,但是我们往往也要处理 Json 类型的返回内容,目的是让数据结构和我们的接口返回的数据结构一致。
- 自定义 servlet 容器
注意的是,Spring Boot 2.x 和 Sprig Boot 1.x 是不一样的。
@Configuration
public class ContainerConfig {
/*- 下面是 springboot 2.x 系列的写法 */
@Bean
public WebServerFactoryCustomizer<ConfigurableWebServerFactory> webServerFactoryCustomizer(){
return factory -> {
factory.addErrorPages(new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/error/500"));
factory.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/error/404"));
};
}
/*- 下面是 springboot 1.x 系列的写法 */
/*@Bean
public EmbeddedServletContainerCustomizer containerCustomizer(){
return new EmbeddedServletContainerCustomizer(){
@Override
public void customize(ConfigurableEmbeddedServletContainer container) {
container.addErrorPages(new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/error/500"));
container.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/error/404"));
}
};
}*/
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
- 自定义对应的请求处理类
@Controller
public class MyBasicErrorController extends BasicErrorController {
public MyBasicErrorController() {
super(new DefaultErrorAttributes(), new ErrorProperties());
}
/**
- @Description: 定义500的ModelAndView
- @Param: [request, response]
- @return: org.springframework.web.servlet.ModelAndView
- @Author: Jet.Chen
- @Date: 2019-07-17 21:56
*/
@RequestMapping(produces = "text/html",value = "/500")
public ModelAndView errorHtml500(HttpServletRequest request, HttpServletResponse response) {
response.setStatus(getStatus(request).value());
Map<String, Object> model = getErrorAttributes(request, isIncludeStackTrace(request, MediaType.TEXT_HTML));
model.put("msg","自定义错误信息");
return new ModelAndView("error/500", model);
}
/**
- @Description: 定义500 和 404 的错误JSON信息
- @Param: [request]
- @return: org.springframework.http.ResponseEntity<cn.jetchen.steecrserver.config.STCRResposeData>
- STCRResposeData 为全局统一的接口数据结构
- @Author: Jet.Chen
- @Date: 2019-07-17 23:13
*/
@RequestMapping(value = {"/500", "/404"})
@ResponseBody
public ResponseEntity<STCRResposeData> error500(HttpServletRequest request) {
Map<String, Object> body = getErrorAttributes(request, isIncludeStackTrace(request, MediaType.TEXT_HTML));
HttpStatus status = getStatus(request);
Object messageTemp;
// stcrResposeData 为返回的数据
STCRResposeData stcrResposeData = STCRResposeData.initError(
String.format("%d%d", 1, status.value()),
(messageTemp = body.get("error")) == null ? null : messageTemp.toString(),
new HashMap<String, Object>() {{
put("error", body.get("error"));
put("message", body.get("message"));
}});
return new ResponseEntity<>(stcrResposeData, status);
}
/**
- @Description: 定义404的ModelAndView
- @Param: [request, response]
- @return: org.springframework.web.servlet.ModelAndView
- @Author: Jet.Chen
- @Date: 2019-07-17 23:13
*/
@RequestMapping(produces = "text/html",value = "/404")
public ModelAndView errorHtml400(HttpServletRequest request, HttpServletResponse response) {
response.setStatus(getStatus(request).value());
Map<String, Object> model = getErrorAttributes(request, isIncludeStackTrace(request, MediaType.TEXT_HTML));
model.put("msg","自定义错误信息");
return new ModelAndView("error/404", model);
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# 实现过程剖析
WebMvcConfigurationSupport 类中实例化了 HandlerExceptionResolver
/**
- Returns a {@link HandlerExceptionResolverComposite} containing a list of exception
- resolvers obtained either through {@link #configureHandlerExceptionResolvers} or
- through {@link #addDefaultHandlerExceptionResolvers}.
- <p><strong>Note:</strong> This method cannot be made final due to CGLIB constraints.
- Rather than overriding it, consider overriding {@link #configureHandlerExceptionResolvers}
- which allows for providing a list of resolvers.
*/
@Bean
public HandlerExceptionResolver handlerExceptionResolver() {
List<HandlerExceptionResolver> exceptionResolvers = new ArrayList<>();
configureHandlerExceptionResolvers(exceptionResolvers);
if (exceptionResolvers.isEmpty()) {
addDefaultHandlerExceptionResolvers(exceptionResolvers);
}
extendHandlerExceptionResolvers(exceptionResolvers);
HandlerExceptionResolverComposite composite = new HandlerExceptionResolverComposite();
composite.setOrder(0);
composite.setExceptionResolvers(exceptionResolvers);
return composite;
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
ExceptionHandlerExceptionResolver 实现了 InitializingBean 接口,重写了 afterPropertiesSet 方法:
@Override
public void afterPropertiesSet() {
// Do this first, it may add ResponseBodyAdvice beans
initExceptionHandlerAdviceCache();
if (this.argumentResolvers == null) {
List<HandlerMethodArgumentResolver> resolvers = getDefaultArgumentResolvers();
this.argumentResolvers = new HandlerMethodArgumentResolverComposite().addResolvers(resolvers);
}
if (this.returnValueHandlers == null) {
List<HandlerMethodReturnValueHandler> handlers = getDefaultReturnValueHandlers();
this.returnValueHandlers = new HandlerMethodReturnValueHandlerComposite().addHandlers(handlers);
}
}
private void initExceptionHandlerAdviceCache() {
if (getApplicationContext() == null) {
return;
}
List<ControllerAdviceBean> adviceBeans = ControllerAdviceBean.findAnnotatedBeans(getApplicationContext());
AnnotationAwareOrderComparator.sort(adviceBeans);
for (ControllerAdviceBean adviceBean : adviceBeans) {
Class<?> beanType = adviceBean.getBeanType();
if (beanType == null) {
throw new IllegalStateException("Unresolvable type for ControllerAdviceBean: " + adviceBean);
}
//重点
ExceptionHandlerMethodResolver resolver = new ExceptionHandlerMethodResolver(beanType);
if (resolver.hasExceptionMappings()) {
this.exceptionHandlerAdviceCache.put(adviceBean, resolver);
}
if (ResponseBodyAdvice.class.isAssignableFrom(beanType)) {
this.responseBodyAdvice.add(adviceBean);
}
}
if (logger.isDebugEnabled()) {
int handlerSize = this.exceptionHandlerAdviceCache.size();
int adviceSize = this.responseBodyAdvice.size();
if (handlerSize == 0 && adviceSize == 0) {
logger.debug("ControllerAdvice beans: none");
}
else {
logger.debug("ControllerAdvice beans: " +
handlerSize + " @ExceptionHandler, " + adviceSize + " ResponseBodyAdvice");
}
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
最熟悉的调用的入口 DispatcherServlet 类的 doDispatch 方法:-->processDispatchResult
# 配置拦截器
# 0、前言
在javaee中,我们经常使用filter来做拦截器,后来有了springmvc,我们使用HandlerInterceptor进行拦截,springmvc的拦截器查看这篇文章,现在有了springboot,我们使用HandlerInterceptor进行拦截,但是我们不用xml的配置,省了很多的事情
# 1、配置拦截器
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import com.me.constant.ConstantEnum;
public class LoginIntercepter implements HandlerInterceptor {
private Logger log = LoggerFactory.getLogger(LoginIntercepter.class);
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
// TODO Auto-generated method stub
log.debug("LoginIntercepter----->pre");
Object userName = request.getSession().getAttribute(ConstantEnum.CURRENT_USER.getIndex());
if(userName == null) {
request.getRequestDispatcher("/login").forward(request, response);
return false;
}
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
// TODO Auto-generated method stub
log.debug("LoginIntercepter----->post");
HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
throws Exception {
// TODO Auto-generated method stub
log.debug("LoginIntercepter----->after");
HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# 2、拦截器注册
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class SlfWebMvcConfiguer implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
// TODO Auto-generated method stub
registry.addInterceptor(new LoginIntercepter()).addPathPatterns("/*Controller/**","/index").excludePathPatterns("/userController/validateLogin","/userController/register");//.excludePathPatterns("/login","/login.html");
WebMvcConfigurer.super.addInterceptors(registry);
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
上述之添加了部分拦截,即controller里的相关方法,如果写成这样addPathPatterns("/**")则是添加了所有的都拦截,连静态资源都会被拦截,即使跳转到登录界面也会加载不出样式。
excludePathPatterns("/userController/validateLogin","/userController/register")
排除了两个不拦截项,
注意注册时的区别
registry.addInterceptor(getInterfaceAuthCheckInterceptor()).addPathPatterns("/api/**"):
这种方式无论什么情况都可以
registry.addInterceptor(new InterfaceAuthCheckInterceptor()).addPathPatterns("/api/**"):
这种情况时,自定义的interceptor中不能注入其他内容,比如redis或者其他service,如果要注入,必须使用上面这种方法
# 3、拦截器中注入service报空指针
需要做如下调整
需要在拦截器上加@Component
——————这个可加可不加
@Component
public class LoginIntercepter implements HandlerInterceptor {
private static Logger log = LoggerFactory.getLogger(LoginIntercepter.class);
//想要注入的service
@Autowired
private UserFileService userFileService;
2
3
4
5
6
进行拦截器配置
//以下很关键,将拦截器添加到配置中去——这才是最关键
@Bean
public LoginIntercepter getMyLoginIntercepter() {
return new LoginIntercepter();
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
// TODO Auto-generated method stub
registry.addInterceptor(getMyLoginIntercepter()).addPathPatterns("/*Controller/**","/index").excludePathPatterns("/userController/validateLogin","/userController/register");//.excludePathPatterns("/login","/login.html");
WebMvcConfigurer.super.addInterceptors(registry);
}
2
3
4
5
6
7
8
9
10
11
这样就可以在拦截器中注入service和其他带有注解的类了,而不会再出现注入的注解为空的情况了
# session共享简单实现
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
2
3
4
5
6
7
8
9
10
11
12
#redis服务器配置
spring.redis.host=localhost
spring.redis.port=6379
spring.redis.password=
spring.redis.database=0
2
3
4
5
注意:
这里我使用的 Spring Boot 版本是2.1.4,如果使用当前最新版Spring Boot2.1.5的话,除了上面这些依赖之外,需要额外添加Spring Security依赖(其他操作不受影响,仅仅只是多了一个依赖,当然也多了 Spring Security 的一些默认认证流程)。之后的2.1.6,2.1.7就无此问题,只有2.1.5有问题
使用
@Slf4j
@RestController
@RequestMapping("/session")
@Api(value = "session共享测试相关", tags = {"session共享测试相关"})
public class FirstChlmController {
@Value("${server.port}")
private String port;
@GetMapping("/setHello")
@ApiOperation(value = "设置name",notes = "向session中设置name值")
@ApiImplicitParams({
@ApiImplicitParam(name = "setName",value = "待设置的值,供session共享",required = true,dataType = "String")})
public String sHello(HttpSession httpSession,@RequestParam("setName") String setName){
httpSession.setAttribute("name", Optional.ofNullable(setName).orElse("陈莉敏"));
return "是"+port+"在写";
}
@GetMapping("/getHello")
@ApiOperation(value = "获取session中的值",notes = "获取session中的值,确保共享成功")
public String gHello(HttpSession httpSession){
return httpSession.getAttribute("name")+"你好"+port;
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 引入Nginx
实现集群D:\GreenApp\nginx-1.16.0\conf\nginx.conf
upstream clm.com{
server 127.0.0.1:8088 weight=1;
server 127.0.0.1:8099 weight=2;
}
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://clm.com;
proxy_redirect default;
root html;
index index.html index.htm;
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
- 在这段配置中:
解析上述配置
- 1、upstream 表示配置上游服务器
- 2、javaboy.org 表示服务器集群的名字,这个可以随意取名字
- 3、upstream 里边配置的是一个个的单独服务
- 4、weight 表示服务的权重,意味者将有多少比例的请求从 Nginx 上转发到该服务上
- 5、location 中的
proxy_pass表示请求转发的地址,/表示拦截到所有的请求,转发转发到刚刚配置好的服务集群中 - 6、proxy_redirect 表示设置当发生重定向请求时,nginx 自动修正响应头数据(默认是 Tomcat 返回重定向,此时重定向的地址是 Tomcat 的地址,我们需要将之修改使之成为 Nginx 的地址)。
访问如下地址即可:http://localhost/chlm/setHello?setName=HTRING
# 引入第三方jar包
<dependency>
<groupId>com.video</groupId>
<artifactId>ysmq-sdk</artifactId>
<version>1.0.3</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/jar/ysmq-subscribe-sdk-1.0.3-RELEASE.jar</systemPath>
</dependency>
2
3
4
5
6
7
其中:groupId与artifactId与version随便写即可
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<includeSystemScope>true</includeSystemScope>
</configuration>
</plugin>
2
3
4
5
6
7
ctrl+alt+shift+s,打开项目设置,添加Libraries