Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 7e716bb

Browse files
web
1 parent c69cb5a commit 7e716bb

File tree

10 files changed

+511
-22
lines changed

10 files changed

+511
-22
lines changed

‎build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ dependencies {
3030
compile('org.springframework.boot:spring-boot-starter-web')
3131
compile('org.springframework.boot:spring-boot-starter-aop')
3232
compile('com.alibaba:druid-spring-boot-starter:1.1.6')
33+
compile 'com.alibaba:fastjson:1.2.55'
3334
runtime('mysql:mysql-connector-java')
3435
testCompile('org.springframework.boot:spring-boot-starter-test')
3536
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package cn.com.hellowood.dynamicdatasource.apiutil.annotation;
2+
3+
import com.alibaba.fastjson.serializer.SerializerFeature;
4+
5+
import java.lang.annotation.*;
6+
7+
/**
8+
* 会在拦截器那里判断是否有这个注解,如果存在把结果包装成 {code:'',data:''} 的形式
9+
*
10+
* @author LDZ
11+
* @date 2020年03月02日 16:36
12+
*/
13+
@Target({ElementType.TYPE, ElementType.METHOD})
14+
@Retention(RetentionPolicy.RUNTIME)
15+
@Documented
16+
public @interface ApiResponseBody {
17+
/**
18+
* 序列化可选 fastjson
19+
*
20+
* @return
21+
* @see SerializerFeature
22+
*/
23+
SerializerFeature[] serializerFeature() default {};
24+
}
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
package cn.com.hellowood.dynamicdatasource.apiutil.config;
2+
3+
4+
import cn.com.hellowood.dynamicdatasource.apiutil.interceptor.ApiResponseBodyReturnValueHandler;
5+
import com.alibaba.fastjson.support.config.FastJsonConfig;
6+
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
7+
import org.springframework.format.FormatterRegistry;
8+
import org.springframework.http.MediaType;
9+
import org.springframework.http.converter.HttpMessageConverter;
10+
import org.springframework.validation.MessageCodesResolver;
11+
import org.springframework.validation.Validator;
12+
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
13+
import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
14+
import org.springframework.web.servlet.HandlerExceptionResolver;
15+
import org.springframework.web.servlet.config.annotation.*;
16+
17+
import java.util.Collections;
18+
import java.util.List;
19+
20+
21+
/**
22+
* spring boot web 通用配置
23+
*
24+
* @author LDZ
25+
* @date 2020年03月02日 17:12
26+
*/
27+
public abstract class BaseWebMvcConfig implements WebMvcConfigurer {
28+
29+
@Override
30+
public void addReturnValueHandlers(List<HandlerMethodReturnValueHandler> returnValueHandlers) {
31+
returnValueHandlers.add(new ApiResponseBodyReturnValueHandler());
32+
}
33+
34+
@Override
35+
public void addInterceptors(InterceptorRegistry registry) {
36+
}
37+
38+
@Override
39+
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
40+
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
41+
FastJsonConfig fastJsonConfig = new FastJsonConfig();
42+
43+
fastJsonConfig.setSerializerFeatures(
44+
// SerializerFeature.PrettyFormat
45+
);
46+
47+
fastConverter.setFastJsonConfig(fastJsonConfig);
48+
fastConverter.setSupportedMediaTypes(Collections.singletonList(MediaType.APPLICATION_JSON_UTF8));
49+
converters.add(0, fastConverter);
50+
51+
}
52+
53+
@Override
54+
public void configurePathMatch(PathMatchConfigurer configurer) {
55+
56+
}
57+
58+
@Override
59+
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
60+
61+
}
62+
63+
@Override
64+
public void configureAsyncSupport(AsyncSupportConfigurer configurer) {
65+
66+
}
67+
68+
@Override
69+
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
70+
71+
}
72+
73+
@Override
74+
public void addFormatters(FormatterRegistry registry) {
75+
76+
}
77+
78+
@Override
79+
public void addResourceHandlers(ResourceHandlerRegistry registry) {
80+
81+
}
82+
83+
@Override
84+
public void addCorsMappings(CorsRegistry registry) {
85+
86+
}
87+
88+
@Override
89+
public void addViewControllers(ViewControllerRegistry registry) {
90+
91+
}
92+
93+
@Override
94+
public void configureViewResolvers(ViewResolverRegistry registry) {
95+
96+
}
97+
98+
@Override
99+
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
100+
101+
}
102+
103+
@Override
104+
public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
105+
106+
}
107+
108+
@Override
109+
public void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers) {
110+
111+
}
112+
113+
@Override
114+
public void extendHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers) {
115+
116+
}
117+
118+
@Override
119+
public Validator getValidator() {
120+
return null;
121+
}
122+
123+
@Override
124+
public MessageCodesResolver getMessageCodesResolver() {
125+
return null;
126+
}
127+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package cn.com.hellowood.dynamicdatasource.apiutil.exception;
2+
3+
import cn.com.hellowood.dynamicdatasource.apiutil.model.BaseResponse;
4+
import org.slf4j.Logger;
5+
import org.slf4j.LoggerFactory;
6+
import org.springframework.validation.BindException;
7+
import org.springframework.web.bind.MissingServletRequestParameterException;
8+
import org.springframework.web.bind.annotation.ControllerAdvice;
9+
import org.springframework.web.bind.annotation.ExceptionHandler;
10+
import org.springframework.web.bind.annotation.ResponseBody;
11+
12+
import javax.servlet.http.HttpServletRequest;
13+
import javax.servlet.http.HttpServletResponse;
14+
import java.util.Optional;
15+
import java.util.stream.Collectors;
16+
17+
import static cn.com.hellowood.dynamicdatasource.apiutil.exception.enums.CustomExceptionEnum.PARAM_ERROR;
18+
19+
/**
20+
* 错误处理句柄
21+
*
22+
* @author LDZ
23+
* @date 2020年03月02日 17:19
24+
*/
25+
26+
@ControllerAdvice
27+
public class BaseExceptionHandler {
28+
29+
private static final Logger log = LoggerFactory.getLogger(BaseExceptionHandler.class);
30+
31+
/**
32+
* @param request 请求
33+
* @param response 返回
34+
* @param handler 句柄
35+
* @param ex 错误
36+
* @return 统一封装返回值
37+
*/
38+
@ResponseBody
39+
@ExceptionHandler(MissingServletRequestParameterException.class)
40+
public BaseResponse argumentMissingError(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
41+
return PARAM_ERROR.handlerBaseResponse("参数错误");
42+
}
43+
44+
@ResponseBody
45+
@ExceptionHandler(BindException.class)
46+
public BaseResponse bindError(HttpServletRequest request, HttpServletResponse response, Object handler, BindException ex) {
47+
String errMessage = ex.getFieldErrors().stream().map(fieldError -> fieldError.getField() + ":" + fieldError.getDefaultMessage()).collect(Collectors.joining(","));
48+
log.warn("server param error ", ex);
49+
return PARAM_ERROR.handlerBaseResponse(errMessage);
50+
51+
}
52+
53+
54+
/**
55+
* 抓取所有的错误
56+
*
57+
* @param request
58+
* @param response
59+
* @param handler
60+
* @param ex
61+
* @return
62+
*/
63+
@ResponseBody
64+
@ExceptionHandler(Exception.class)
65+
public BaseResponse defaultErrorHandle(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
66+
log.error("server error ", ex);
67+
return PARAM_ERROR.handlerBaseResponse("服务器繁忙");
68+
}
69+
70+
71+
@ResponseBody
72+
@ExceptionHandler(CustomServiceException.class)
73+
public BaseResponse customExceptionHandle(HttpServletRequest request, HttpServletResponse response, Object handler, CustomServiceException customServiceException) {
74+
75+
log.debug("business err {} ", customServiceException.getErrorDescription());
76+
return customServiceException.getCustomExceptionEnum().handlerBaseResponse(
77+
Optional.ofNullable(customServiceException.getErrorDescription()).orElse("服务器繁忙"),
78+
Optional.ofNullable(customServiceException.getData()).orElse(null));
79+
80+
81+
}
82+
83+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package cn.com.hellowood.dynamicdatasource.apiutil.exception;
2+
3+
/**
4+
* @author XiaoLei
5+
* @date 2018年4月17日 15:19
6+
* @description
7+
*/
8+
9+
import cn.com.hellowood.dynamicdatasource.apiutil.exception.enums.CustomExceptionEnum;
10+
11+
/**
12+
* 自定义异常
13+
*
14+
* @author LDZ
15+
* @date 2020年03月02日 17:22
16+
*/
17+
public class CustomServiceException extends RuntimeException {
18+
19+
/**
20+
* 自定义错误
21+
*/
22+
private CustomExceptionEnum customExceptionEnum;
23+
24+
/**
25+
* 错误的描述
26+
*/
27+
private String errorDescription;
28+
29+
/**
30+
* 需要返回的数据
31+
*/
32+
private Object data;
33+
34+
35+
public CustomServiceException(CustomExceptionEnum customExceptionEnum, String desc) {
36+
super(desc);
37+
this.customExceptionEnum = customExceptionEnum;
38+
this.errorDescription = desc;
39+
}
40+
41+
public CustomServiceException(CustomExceptionEnum customExceptionEnum, String desc, Object data) {
42+
super(desc);
43+
this.customExceptionEnum = customExceptionEnum;
44+
this.errorDescription = desc;
45+
this.data = data;
46+
}
47+
48+
public CustomExceptionEnum getCustomExceptionEnum() {
49+
return customExceptionEnum;
50+
}
51+
52+
public void setCustomExceptionEnum(CustomExceptionEnum customExceptionEnum) {
53+
this.customExceptionEnum = customExceptionEnum;
54+
}
55+
56+
public String getErrorDescription() {
57+
return errorDescription;
58+
}
59+
60+
public void setErrorDescription(String errorDescription) {
61+
this.errorDescription = errorDescription;
62+
}
63+
64+
public Object getData() {
65+
return data;
66+
}
67+
68+
public void setData(Object data) {
69+
this.data = data;
70+
}
71+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package cn.com.hellowood.dynamicdatasource.apiutil.exception.enums;
2+
3+
import cn.com.hellowood.dynamicdatasource.apiutil.exception.CustomServiceException;
4+
import cn.com.hellowood.dynamicdatasource.apiutil.model.BaseResponse;
5+
import jdk.nashorn.internal.objects.annotations.Getter;
6+
7+
import java.util.function.BiFunction;
8+
9+
/**
10+
* @author LDZ
11+
* @date 2020年03月02日 17:23
12+
*/
13+
public enum CustomExceptionEnum {
14+
15+
16+
/**
17+
* 业务的参数错误
18+
*/
19+
PARAM_ERROR(105) {
20+
// override
21+
22+
};
23+
24+
25+
int code;
26+
27+
public RuntimeException handlerException(String s, Object d) {
28+
return new CustomServiceException(this, s, d);
29+
}
30+
31+
public RuntimeException handlerException(String s) {
32+
return new CustomServiceException(this, s);
33+
}
34+
35+
public BaseResponse handlerBaseResponse(String s, Object d) {
36+
return new BaseResponse(this, s, d);
37+
}
38+
39+
public BaseResponse handlerBaseResponse(String s) {
40+
return new BaseResponse(this, s);
41+
}
42+
43+
public int getCode() {
44+
return code;
45+
}
46+
47+
CustomExceptionEnum(int code) {
48+
this.code = code;
49+
}
50+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /