Commit 74df7267 by qiuweili123

merge starter

parent b1975ace
package com.secoo.mall.dubbo.annotation;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.core.annotation.AliasFor;
import java.lang.annotation.*;
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Service
public @interface DubboService {
@AliasFor(
annotation = Service.class
)
String value() default "";
}
package com.secoo.mall.common.handler;
import com.secoo.mall.common.core.errorcode.CommonErrorCode;
import com.secoo.mall.common.core.exception.BusinessException;
import com.secoo.mall.common.core.exception.ParameterException;
import com.secoo.mall.common.core.exception.SystemInternalException;
import com.secoo.mall.common.util.log.LoggerUtil;
import com.secoo.mall.common.util.response.ResponseUtil;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.web.bind.annotation.ExceptionHandler;
import javax.annotation.Resource;
public class ProtocolExceptionHandler {
@Resource
private MessageSource messageSource;
@ExceptionHandler(BusinessException.class)
public Object businessExcepstionHandler(BusinessException e) {
LoggerUtil.warn("businessException info:", e);
return ResponseUtil.getFailResponse(e.getCode(), getMsg(e));
}
@ExceptionHandler(ParameterException.class)
public Object parameterExceptionHandler(ParameterException e) {
String msg = getMsg(e);
LoggerUtil.info(msg);
return ResponseUtil.getFailResponse(e.getCode(), msg);
}
@ExceptionHandler({SystemInternalException.class, Exception.class})
public Object exceptionHandler(Exception e) {
LoggerUtil.error(e);
return ResponseUtil.getFailResponse(CommonErrorCode.SYSTEM_INTERNAL_EXCEPTION.getCode(), CommonErrorCode.SYSTEM_INTERNAL_EXCEPTION.getMsg());
}
private String getMsg(BusinessException e) {
return messageSource.getMessage(e.getMsg(), e.getArgs(), LocaleContextHolder.getLocale());
}
}
package com.secoo.mall.common.provider;
import org.springframework.context.annotation.Primary;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import springfox.documentation.spring.web.DocumentationCache;
import springfox.documentation.swagger.web.InMemorySwaggerResourcesProvider;
import springfox.documentation.swagger.web.SwaggerResource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
@Primary
@Component
public class DefaultSwaggerResourcesProvider extends InMemorySwaggerResourcesProvider {
public DefaultSwaggerResourcesProvider(Environment environment, DocumentationCache documentationCache) {
super(environment, documentationCache);
}
@Override
public List<SwaggerResource> get() {
List<SwaggerResource> swaggerResources = super.get();
swaggerResources.forEach(swaggerResource -> {
String name = swaggerResource.getName();
if (SwaggerConfigEnum.SwaggerConfigMap.containsKey(name)) {
swaggerResource.setLocation(SwaggerConfigEnum.SwaggerConfigMap.get(name).getLocation());
}
}
);
return swaggerResources;
}
private enum SwaggerConfigEnum {
DUBBO("dubbo", "/swagger-dubbo/api-docs");
private String name;
private String location;
private final static Map<String, SwaggerConfigEnum> SwaggerConfigMap = Arrays.stream(SwaggerConfigEnum.values()).collect(Collectors.toMap(SwaggerConfigEnum::getName, Function.identity()));
SwaggerConfigEnum(String name, String location) {
this.name = name;
this.location = location;
}
public String getName() {
return name;
}
public String getLocation() {
return location;
}
}
}
package com.secoo.mall.web.config; package com.secoo.mall.config;
import com.deepoove.swagger.dubbo.annotations.EnableDubboSwagger;
import com.github.xiaoymin.swaggerbootstrapui.annotations.EnableSwaggerBootstrapUI; import com.github.xiaoymin.swaggerbootstrapui.annotations.EnableSwaggerBootstrapUI;
import com.secoo.mall.common.core.condition.ProdEnvCondition; import com.secoo.mall.common.core.condition.ProdEnvCondition;
import com.secoo.mall.web.annotation.ApiController; import com.secoo.mall.web.annotation.ApiController;
...@@ -18,6 +19,7 @@ import springfox.documentation.swagger2.annotations.EnableSwagger2; ...@@ -18,6 +19,7 @@ import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration @Configuration
@EnableSwagger2 @EnableSwagger2
@EnableSwaggerBootstrapUI @EnableSwaggerBootstrapUI
@EnableDubboSwagger
@Conditional(ProdEnvCondition.class) @Conditional(ProdEnvCondition.class)
public class SwaggerConfig { public class SwaggerConfig {
...@@ -27,16 +29,27 @@ public class SwaggerConfig { ...@@ -27,16 +29,27 @@ public class SwaggerConfig {
private String appVersion; private String appVersion;
@Bean @Bean
public Docket createRestApi() { public Docket createWebApi() {
return new Docket(DocumentationType.SWAGGER_2) return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo()) .apiInfo(apiInfo())
.groupName("SecooMall") .groupName("web")
.select() .select()
.apis(RequestHandlerSelectors.withClassAnnotation(ApiController.class)) .apis(RequestHandlerSelectors.withClassAnnotation(ApiController.class))
.paths(PathSelectors.any()) .paths(PathSelectors.any())
.build(); .build();
} }
public Docket createDubboApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.groupName("dubbo")
.select()
.apis(RequestHandlerSelectors.basePackage("com.deepoove.dubbo.provider.springboot"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() { private ApiInfo apiInfo() {
return new ApiInfoBuilder() return new ApiInfoBuilder()
.title(appName + " RESTful APIs") .title(appName + " RESTful APIs")
......
package com.secoo.mall.dubbo.filter;
import com.secoo.mall.common.core.exception.SystemInternalException;
import com.secoo.mall.common.handler.ProtocolExceptionHandler;
import com.secoo.mall.common.util.response.ResponseUtil;
import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.rpc.*;
import org.apache.dubbo.rpc.filter.ExceptionFilter;
import org.apache.dubbo.rpc.service.GenericService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.method.annotation.ExceptionHandlerMethodResolver;
import java.lang.reflect.Method;
@Activate(
group = {CommonConstants.PROVIDER}
)
public class DefaultExceptionFilter extends ExceptionFilter implements Filter {
private Logger log = LoggerFactory.getLogger(DefaultExceptionFilter.class);
public DefaultExceptionFilter() {
super.listener = new DefaultExceptionFilter.ExceptionListener();
}
static class ExceptionListener extends ProtocolExceptionHandler implements Listener {
private Logger logger = LoggerFactory.getLogger(ExceptionListener.class);
private ExceptionHandlerMethodResolver resolver;
public ExceptionListener() {
resolver = new ExceptionHandlerMethodResolver(this.getClass());
}
@Override
public void onResponse(Result appResponse, Invoker<?> invoker, Invocation invocation) {
if (appResponse.hasException() && GenericService.class != invoker.getInterface()) {
Exception exception = (Exception) appResponse.getException();
Method method = resolver.resolveMethod(exception);
try {
Object realResult = method.invoke(this, exception);
appResponse.setValue(realResult);
} catch (Exception e) {
appResponse.setValue(ResponseUtil.getFailResponse(new SystemInternalException()));
}
appResponse.setException(null);
}
}
@Override
public void onError(Throwable e, Invoker<?> invoker, Invocation invocation) {
logger.error("Got unchecked and undeclared exception which called by " + RpcContext.getContext().getRemoteHost() + ". service: " + invoker.getInterface().getName() + ", method: " + invocation.getMethodName() + ", exception: " + e.getClass().getName() + ": " + e.getMessage(), e);
}
}
}
package com.secoo.mall.web.advice; package com.secoo.mall.web.advice;
import com.secoo.mall.common.core.errorcode.CommonErrorCode;
import com.secoo.mall.common.core.exception.BusinessException;
import com.secoo.mall.common.core.exception.ParameterException;
import com.secoo.mall.common.core.exception.SystemInternalException;
import com.secoo.mall.common.handler.ProtocolExceptionHandler; import com.secoo.mall.common.handler.ProtocolExceptionHandler;
import com.secoo.mall.common.util.log.LoggerUtil;
import com.secoo.mall.common.util.response.ResponseUtil; import com.secoo.mall.common.util.response.ResponseUtil;
import com.secoo.mall.web.annotation.ApiController; import com.secoo.mall.web.annotation.ApiController;
import com.secoo.mall.web.annotation.ApiIgnoreJson; import com.secoo.mall.web.annotation.ApiIgnoreJson;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.core.MethodParameter; import org.springframework.core.MethodParameter;
import org.springframework.core.annotation.AnnotationUtils; import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter; import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.server.ServerHttpRequest; import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse; import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice; import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice; import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
import javax.annotation.Resource;
@RestControllerAdvice(annotations = ApiController.class) @RestControllerAdvice(annotations = ApiController.class)
public class ControllerResponseAdvice extends ProtocolExceptionHandler implements ResponseBodyAdvice<Object> { public class ControllerResponseAdvice extends ProtocolExceptionHandler implements ResponseBodyAdvice<Object> {
/*
@Resource
private MessageSource messageSource;
*/
/* @ExceptionHandler(BusinessException.class)
public Object businessExcepstionHandler(BusinessException e) {
LoggerUtil.warn("businessException info:", e);
return ResponseUtil.getFailResponse(e.getCode(), getMsg(e));
}
@ExceptionHandler(ParameterException.class)
public Object parameterExceptionHandler(ParameterException e) {
String msg = getMsg(e);
LoggerUtil.info(msg);
return ResponseUtil.getFailResponse(e.getCode(), msg);
}
@ExceptionHandler({SystemInternalException.class, Exception.class})
public Object exceptionHandler(Exception e) {
LoggerUtil.error(e);
return ResponseUtil.getFailResponse(CommonErrorCode.SYSTEM_INTERNAL_EXCEPTION.getCode(), CommonErrorCode.SYSTEM_INTERNAL_EXCEPTION.getMsg());
}*/
@Override @Override
public boolean supports(MethodParameter methodParameter, Class<? extends HttpMessageConverter<?>> aClass) { public boolean supports(MethodParameter methodParameter, Class<? extends HttpMessageConverter<?>> aClass) {
return AnnotationUtils.findAnnotation(methodParameter.getMethod(), ApiIgnoreJson.class) == null; return AnnotationUtils.findAnnotation(methodParameter.getMethod(), ApiIgnoreJson.class) == null;
...@@ -59,7 +26,4 @@ public class ControllerResponseAdvice extends ProtocolExceptionHandler impleme ...@@ -59,7 +26,4 @@ public class ControllerResponseAdvice extends ProtocolExceptionHandler impleme
return ResponseUtil.getSuccessResponse(o); return ResponseUtil.getSuccessResponse(o);
} }
/* private String getMsg(BusinessException e) {
return messageSource.getMessage(e.getMsg(), e.getArgs(), LocaleContextHolder.getLocale());
}*/
} }
com.secoo.mall.dubbo.filter.DefaultExceptionFilter
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment