From 0b49362d7a6d5c742b9e585aeb5e5e11bd4dd111 Mon Sep 17 00:00:00 2001 From: Tang Cheng Date: Tue, 17 Mar 2020 10:46:32 +0800 Subject: [PATCH] =?utf8?q?feat:=20=E4=BC=98=E5=8C=96=E7=B3=BB=E7=BB=9F?= =?utf8?q?=E5=A4=84=E7=90=86=20responsestatus=20=E5=BC=82=E5=B8=B8?= =?utf8?q?=E7=9A=84=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../com/supwisdom/dlpay/PayApiApplication.kt | 12 +++++++++ .../kotlin/com/supwisdom/dlpay/api/advices.kt | 27 ++++++++++++++----- .../controller/security_controller.kt | 2 +- 3 files changed, 34 insertions(+), 7 deletions(-) diff --git a/payapi/src/main/kotlin/com/supwisdom/dlpay/PayApiApplication.kt b/payapi/src/main/kotlin/com/supwisdom/dlpay/PayApiApplication.kt index be0b03e6..b09c0847 100644 --- a/payapi/src/main/kotlin/com/supwisdom/dlpay/PayApiApplication.kt +++ b/payapi/src/main/kotlin/com/supwisdom/dlpay/PayApiApplication.kt @@ -23,6 +23,7 @@ import org.springframework.boot.builder.SpringApplicationBuilder import org.springframework.boot.context.properties.ConfigurationProperties import org.springframework.boot.web.servlet.FilterRegistrationBean import org.springframework.boot.web.servlet.ServletComponentScan +import org.springframework.boot.web.servlet.error.DefaultErrorAttributes import org.springframework.boot.web.servlet.support.SpringBootServletInitializer import org.springframework.cache.annotation.EnableCaching import org.springframework.cache.interceptor.KeyGenerator @@ -32,6 +33,8 @@ import org.springframework.context.annotation.ComponentScan import org.springframework.context.annotation.Configuration import org.springframework.context.event.ContextStartedEvent import org.springframework.context.event.EventListener +import org.springframework.core.Ordered +import org.springframework.core.annotation.Order import org.springframework.data.redis.connection.RedisConnectionFactory import org.springframework.data.redis.connection.RedisPassword import org.springframework.data.redis.connection.RedisStandaloneConfiguration @@ -90,6 +93,12 @@ class AppConfig { fun lockProvider(connectionFactory: RedisConnectionFactory): LockProvider { return RedisLockProvider(connectionFactory, "prod") } + + @Bean + @Order(Ordered.HIGHEST_PRECEDENCE) + fun getDefaultErrorAttributes(): DefaultErrorAttributes { + return DefaultErrorAttributes(false) + } } @Configuration @@ -141,8 +150,10 @@ class RestTemplateConfig { class RestTemplateProxyProperties { @Value("\${type:}") lateinit var type: String + @Value("\${host:}") lateinit var host: String + @Value("\${port:0}") var port: Int = 0 } @@ -179,6 +190,7 @@ class RestTemplateConfig { @Component class TenantConfigListener { private val log = KotlinLogging.logger { } + @EventListener fun handleContextStarted(evt: ContextStartedEvent) { log.info { "Service started check tenant information" } diff --git a/payapi/src/main/kotlin/com/supwisdom/dlpay/api/advices.kt b/payapi/src/main/kotlin/com/supwisdom/dlpay/api/advices.kt index 47182ef7..a939feb1 100644 --- a/payapi/src/main/kotlin/com/supwisdom/dlpay/api/advices.kt +++ b/payapi/src/main/kotlin/com/supwisdom/dlpay/api/advices.kt @@ -5,7 +5,6 @@ import com.supwisdom.dlpay.api.exception.RequestParamCheckException import com.supwisdom.dlpay.exception.TransactionException import com.supwisdom.dlpay.framework.ResponseBodyBuilder import com.supwisdom.dlpay.framework.service.CommonService -import com.supwisdom.dlpay.framework.util.TradeErrorCode import mu.KotlinLogging import org.aspectj.lang.ProceedingJoinPoint import org.aspectj.lang.annotation.Around @@ -13,18 +12,20 @@ import org.aspectj.lang.annotation.Aspect import org.aspectj.lang.annotation.Pointcut import org.springframework.beans.factory.annotation.Autowired import org.springframework.beans.factory.annotation.Value +import org.springframework.boot.web.servlet.error.ErrorAttributes +import org.springframework.core.annotation.AnnotationUtils import org.springframework.http.HttpHeaders import org.springframework.http.HttpStatus import org.springframework.http.ResponseEntity import org.springframework.security.core.context.SecurityContextHolder import org.springframework.stereotype.Component import org.springframework.transaction.TransactionSystemException -import org.springframework.validation.annotation.Validated import org.springframework.web.bind.MethodArgumentNotValidException import org.springframework.web.bind.annotation.ControllerAdvice import org.springframework.web.bind.annotation.ExceptionHandler import org.springframework.web.bind.annotation.ResponseStatus import org.springframework.web.bind.annotation.RestControllerAdvice +import org.springframework.web.context.request.ServletWebRequest import org.springframework.web.context.request.WebRequest import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler import java.lang.reflect.UndeclaredThrowableException @@ -33,11 +34,21 @@ import javax.validation.ConstraintViolationException @RestControllerAdvice("com.supwisdom.dlpay.api") -class RestControllerAdvice { +class RestControllerAdvice(private val errorAttributes: ErrorAttributes) { val logger = KotlinLogging.logger { } + protected fun getStatus(request: HttpServletRequest): HttpStatus { + val statusCode = request.getAttribute("javax.servlet.error.status_code") + ?: return HttpStatus.INTERNAL_SERVER_ERROR + return try { + HttpStatus.valueOf(statusCode as Int) + } catch (ex: java.lang.Exception) { + HttpStatus.INTERNAL_SERVER_ERROR + } + } + @ExceptionHandler - fun handleException(ex: Exception, request: HttpServletRequest): ResponseEntity { + fun handleException(ex: Exception, request: HttpServletRequest): ResponseEntity<*> { logger.error { "Request unhandler exception, url<${request.requestURI}>, ex<${ex.cause}>" } val undeclared = if (ex is UndeclaredThrowableException) ex.undeclaredThrowable else ex if (undeclared is RequestParamCheckException) { @@ -48,8 +59,11 @@ class RestControllerAdvice { .exception(undeclared.code(), undeclared, "业务处理错误")) } - return ResponseEntity.ok().body(ResponseBodyBuilder.create() - .exception(TradeErrorCode.BUSINESS_DEAL_ERROR, undeclared, "业务处理报错")) + val body = this.errorAttributes.getErrorAttributes(ServletWebRequest(request), + false) + val responseStatus = AnnotationUtils.findAnnotation(ex.javaClass, ResponseStatus::class.java) + val status = responseStatus?.code ?: getStatus(request) + return ResponseEntity>(body, status) } } @@ -57,6 +71,7 @@ class RestControllerAdvice { @ControllerAdvice("com.supwisdom.dlpay") class MvcControllerAdvice { private val logger = KotlinLogging.logger { } + @ExceptionHandler(ConstraintViolationException::class) fun handleException(e: ConstraintViolationException): ResponseEntity { logger.error { "unhandle exception : $e" } diff --git a/payapi/src/main/kotlin/com/supwisdom/dlpay/framework/controller/security_controller.kt b/payapi/src/main/kotlin/com/supwisdom/dlpay/framework/controller/security_controller.kt index 97e1782e..baac6565 100644 --- a/payapi/src/main/kotlin/com/supwisdom/dlpay/framework/controller/security_controller.kt +++ b/payapi/src/main/kotlin/com/supwisdom/dlpay/framework/controller/security_controller.kt @@ -128,7 +128,7 @@ class ApiAuthController { .success(ApiLoginResponse(token.get().jwt, appid, Instant.ofEpochSecond(token.get().expiration).toString()))) } else { - ResponseEntity.status(HttpStatus.UNAUTHORIZED).build() + ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("token verify error") } } } -- 2.17.1