Skip to content

Commit 6e3a5e3

Browse files
committed
重构 Web 模块边界
1 parent 6933c90 commit 6e3a5e3

17 files changed

Lines changed: 75 additions & 193 deletions

File tree

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
11
package io.github.opensabre.boot.config;
22

3-
import io.github.opensabre.common.web.exception.DefaultGlobalExceptionHandlerAdvice;
4-
import io.github.opensabre.common.web.rest.RestResponseBodyAdvice;
53
import org.springframework.boot.autoconfigure.AutoConfiguration;
6-
import org.springframework.context.annotation.Import;
74
import org.springframework.context.annotation.PropertySource;
85

96
/**
10-
* 初使化全局报文和全局异常配置
7+
* 初使化 Boot 通用配置
118
*/
129
@AutoConfiguration
1310
@PropertySource(value = "classpath:opensabre-boot.properties", encoding = "UTF8")
14-
@Import({DefaultGlobalExceptionHandlerAdvice.class, RestResponseBodyAdvice.class})
1511
public class OpensabreBootConfig {
1612
}

opensabre-starter-boot/src/main/java/io/github/opensabre/boot/config/OpensabreWebConfiguration.java

Lines changed: 0 additions & 11 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
io.github.opensabre.boot.config.OpensabreSwaggerConfig
22
io.github.opensabre.boot.config.OpensabreBootConfig
3-
io.github.opensabre.boot.config.OpensabreWebConfiguration
43
io.github.opensabre.boot.config.OpensabreServiceConfig
54
io.github.opensabre.boot.config.OpensabreSensitiveConfig

opensabre-starter-webflux/pom.xml

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,6 @@
3939
<dependency>
4040
<groupId>io.github.opensabre</groupId>
4141
<artifactId>opensabre-web</artifactId>
42-
<exclusions>
43-
<exclusion>
44-
<groupId>org.springframework</groupId>
45-
<artifactId>spring-webmvc</artifactId>
46-
</exclusion>
47-
<exclusion>
48-
<groupId>jakarta.servlet</groupId>
49-
<artifactId>jakarta.servlet-api</artifactId>
50-
</exclusion>
51-
</exclusions>
5242
</dependency>
5343
<dependency>
5444
<groupId>org.springframework</groupId>

opensabre-starter-webmvc/pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@
7373
<groupId>org.springframework</groupId>
7474
<artifactId>spring-webmvc</artifactId>
7575
</dependency>
76+
<dependency>
77+
<groupId>org.springframework.boot</groupId>
78+
<artifactId>spring-boot-autoconfigure</artifactId>
79+
</dependency>
7680
<!--常用工具包-->
7781
<dependency>
7882
<groupId>org.apache.commons</groupId>

opensabre-starter-webmvc/readme.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ WEBMVC公共包
33

44
## 简介
55

6-
主要封装spring-webmvc WEB开发用到的通用公共类、工具类,如公共web拦截器、web统一异常定义等
6+
主要封装 Spring MVC Web 开发用到的 Servlet/MVC 特性能力,如 MVC 自动装配、MVC 全局异常处理、MVC 统一响应包装、Servlet 用户上下文拦截器等
77

88
## 使用
99

@@ -23,6 +23,6 @@ pom.xml
2323
<dependency>
2424
<groupId>io.github.opensabre</groupId>
2525
<artifactId>opensabre-starter-webmvc</artifactId>
26-
<version>0.1.1</version>
26+
<version>0.3.0</version>
2727
</dependency>
28-
```
28+
```
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package io.github.opensabre.webmvc.config;
2+
3+
import io.github.opensabre.webmvc.exception.DefaultWebMvcExceptionHandlerAdvice;
4+
import io.github.opensabre.webmvc.rest.RestResponseBodyAdvice;
5+
import org.springframework.boot.autoconfigure.AutoConfiguration;
6+
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
7+
import org.springframework.context.annotation.Import;
8+
9+
/**
10+
* Opensabre WebMvc auto-configuration.
11+
*/
12+
@AutoConfiguration
13+
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
14+
@Import({DefaultWebMvcExceptionHandlerAdvice.class, RestResponseBodyAdvice.class})
15+
public class OpensabreWebMvcConfig {
16+
}

opensabre-starter-webmvc/src/main/java/io/github/opensabre/webmvc/exception/DefaultWebMvcExceptionHandlerAdvice.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import io.github.opensabre.common.core.entity.vo.Result;
44
import io.github.opensabre.common.core.exception.SystemErrorType;
55
import jakarta.servlet.ServletException;
6+
import io.github.opensabre.common.core.exception.BaseException;
67
import lombok.extern.slf4j.Slf4j;
78
import org.springframework.core.annotation.Order;
89
import org.springframework.http.HttpStatus;
@@ -40,7 +41,7 @@ public Result<?> argumentInvalidException(MethodArgumentNotValidException ex) {
4041
}
4142

4243
@ExceptionHandler(value = {HttpMessageNotReadableException.class, MethodArgumentTypeMismatchException.class})
43-
public Result<?> httpMessageConvertException(HttpMessageNotReadableException ex) {
44+
public Result<?> httpMessageConvertException(Exception ex) {
4445
log.warn("http message convert exception:{}", ex.getMessage());
4546
return Result.fail(SystemErrorType.ARGUMENT_NOT_VALID, "数据解析错误:" + ex.getMessage());
4647
}
@@ -71,4 +72,17 @@ public Result<?> notSupportedMethodException(HttpMediaTypeNotSupportedException
7172
log.warn("http request media not supported exception {}", ex.getMessage());
7273
return Result.fail(SystemErrorType.METHOD_NOT_SUPPORTED);
7374
}
74-
}
75+
76+
@ExceptionHandler(value = {BaseException.class})
77+
public Result<?> baseException(BaseException ex) {
78+
log.warn("base exception:{}", ex.getMessage());
79+
return Result.fail(ex.getErrorType());
80+
}
81+
82+
@ExceptionHandler(value = {Exception.class, Throwable.class})
83+
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
84+
public Result<?> exception(Throwable ex) {
85+
log.error("exception: ", ex);
86+
return Result.fail();
87+
}
88+
}

opensabre-web/src/main/java/io/github/opensabre/common/web/interceptor/UserInterceptor.java renamed to opensabre-starter-webmvc/src/main/java/io/github/opensabre/webmvc/interceptor/UserInterceptor.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package io.github.opensabre.common.web.interceptor;
1+
package io.github.opensabre.webmvc.interceptor;
22

33
import com.fasterxml.jackson.databind.ObjectMapper;
44
import io.github.opensabre.common.core.util.UserContextHolder;
@@ -8,7 +8,6 @@
88
import org.springframework.lang.Nullable;
99
import org.springframework.web.servlet.HandlerInterceptor;
1010

11-
1211
import java.util.Map;
1312

1413
/**
@@ -29,6 +28,7 @@ public class UserInterceptor implements HandlerInterceptor {
2928
public static final String X_CLIENT_TOKEN = "x-client-token";
3029

3130
@Override
31+
@SuppressWarnings("unchecked")
3232
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
3333
//从网关获取并校验,通过校验就可信任x-client-token-user中的信息
3434
checkToken(request.getHeader(X_CLIENT_TOKEN));
@@ -47,7 +47,7 @@ private void checkToken(String token) {
4747
}
4848

4949
@Override
50-
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable Exception ex) throws Exception {
50+
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable Exception ex) {
5151
UserContextHolder.getInstance().clear();
5252
}
5353
}

opensabre-web/src/main/java/io/github/opensabre/common/web/rest/RestResponseBodyAdvice.java renamed to opensabre-starter-webmvc/src/main/java/io/github/opensabre/webmvc/rest/RestResponseBodyAdvice.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package io.github.opensabre.common.web.rest;
1+
package io.github.opensabre.webmvc.rest;
22

33
import com.fasterxml.jackson.databind.ObjectMapper;
44
import io.github.opensabre.common.core.entity.vo.Result;
@@ -27,12 +27,12 @@ public class RestResponseBodyAdvice implements ResponseBodyAdvice<Object> {
2727
/**
2828
* 框架中不需要包装为Result对象的包名
2929
*/
30-
@Value("${opensabre.rest.result.framework.excludes}")
30+
@Value("${opensabre.rest.result.framework.excludes:org.springdoc}")
3131
private String excludeFrameworkPackageStr;
3232
/**
3333
* 应用中不需要包装为Result对象的包名
3434
*/
35-
@Value("${opensabre.rest.result.excludes}")
35+
@Value("${opensabre.rest.result.excludes:}")
3636
private String excludePackageStr;
3737

3838
@Override
@@ -77,4 +77,4 @@ public Object beforeBodyWrite(Object body,
7777
}
7878
return Result.success(body);
7979
}
80-
}
80+
}

0 commit comments

Comments
 (0)