Explorar o código

gateway抛出错误

gaodm %!s(int64=6) %!d(string=hai) anos
pai
achega
93308f2af7

+ 2 - 1
common/src/main/java/com/diagbot/exception/CommonErrorCode.java

@@ -19,7 +19,8 @@ public enum CommonErrorCode implements ErrorCode {
     PARAM_IS_NULL("00020004", "传入的参数为空!"),
     PARAM_IS_ERROR("00020005", "传入的参数为错误!"),
     STATUS_IS_ERROR("00020006", "参数状态错误!"),
-    SERVER_IS_ERROR("00020000", "各自业务错误!");
+    SERVER_IS_ERROR("00020007", "各自业务错误!"),
+    NO_PERMISSION("00020008", "无权访问!");
 
     private String code;
     private String msg;

+ 2 - 1
gateway-service/src/main/java/com/diagbot/client/DiagbotmanServiceClient.java

@@ -4,6 +4,7 @@ import com.diagbot.client.hystrix.DiagbotmanServiceHystrix;
 import com.diagbot.dto.RespDTO;
 import org.springframework.cloud.openfeign.FeignClient;
 import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestParam;
 
 import java.util.List;
 
@@ -20,7 +21,7 @@ public interface DiagbotmanServiceClient {
     RespDTO<List<String>> getAll();
 
     @PostMapping("/service_filter/getPermissonUrl")
-    RespDTO<List<String>> getPermissonUrl(String appkey, String secret);
+    RespDTO<List<String>> getPermissonUrl(@RequestParam("appkey")String appkey, @RequestParam("secret")String secret);
 
 }
 

+ 0 - 80
gateway-service/src/main/java/com/diagbot/exception/CommonExceptionHandler.java

@@ -1,80 +0,0 @@
-package com.diagbot.exception;
-
-import com.diagbot.dto.RespDTO;
-import com.diagbot.util.GsonUtil;
-import lombok.extern.slf4j.Slf4j;
-import org.springframework.http.HttpStatus;
-import org.springframework.http.ResponseEntity;
-import org.springframework.validation.BindException;
-import org.springframework.validation.FieldError;
-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.ResponseBody;
-
-import java.util.HashMap;
-import java.util.Map;
-
-
-/**
- * @Description: 错误通用处理
- * @author: gaodm
- * @time: 2018/8/2 14:22
- */
-@ControllerAdvice
-@ResponseBody
-@Slf4j
-public class CommonExceptionHandler {
-
-    @ExceptionHandler(Exception.class)
-    public ResponseEntity<RespDTO> handleException(Exception e) {
-        RespDTO resp = new RespDTO();
-        if(e instanceof BindException) {
-            BindException ex = (BindException) e;
-            Map<String, String> stringMap = new HashMap<>();
-            for (FieldError fieldError : ex.getBindingResult().getFieldErrors()) {
-                stringMap.put(fieldError.getField(), fieldError.getDefaultMessage());
-            }
-            String msg = GsonUtil.toJson(stringMap);
-            log.warn("【参数异常】:{}", msg);
-            resp.code = CommonErrorCode.PARAM_ERROR.getCode();
-            resp.msg = msg;
-            return new ResponseEntity(resp, HttpStatus.OK);
-        }
-        if(e instanceof MethodArgumentNotValidException) {
-            MethodArgumentNotValidException ex = (MethodArgumentNotValidException) e;
-            Map<String, String> stringMap = new HashMap<>();
-            for (FieldError fieldError : ex.getBindingResult().getFieldErrors()) {
-                stringMap.put(fieldError.getField(), fieldError.getDefaultMessage());
-            }
-            String msg = GsonUtil.toJson(stringMap);
-            log.warn("【参数异常】:{}", msg);
-            resp.code = CommonErrorCode.PARAM_ERROR.getCode();
-            resp.msg = msg;
-            return new ResponseEntity(resp, HttpStatus.OK);
-        }
-//        if(e instanceof MissingServletRequestParameterException) {
-//            MissingServletRequestParameterException ex = (MissingServletRequestParameterException) e;
-//            Map<String, String> stringMap = new HashMap<>();
-//            stringMap.put(ex.getParameterName(), "不能为null");
-//            String msg = GsonUtil.toJson(stringMap);
-//            log.warn("【参数异常】:{}", msg);
-//            resp.code = CommonErrorCode.PARAM_ERROR.getCode();
-//            resp.msg = msg;
-//            return new ResponseEntity(resp, HttpStatus.OK);
-//        }
-        if(e instanceof CommonException) {
-            CommonException taiChiException = (CommonException) e;
-            resp.code = taiChiException.getCode();
-            resp.msg = e.getMessage();
-            log.error("【业务异常】:{}", e.getMessage());
-            return new ResponseEntity(resp, HttpStatus.OK);
-        }
-        resp.code = CommonErrorCode.FAIL.getCode();
-        resp.msg = e.getMessage();
-        log.error("【系统异常】:{}", e.getMessage());
-        e.printStackTrace();
-        return new ResponseEntity(resp, HttpStatus.OK);
-    }
-
-}

+ 29 - 0
gateway-service/src/main/java/com/diagbot/filter/GlobalGatewayFilter.java

@@ -2,17 +2,26 @@ package com.diagbot.filter;
 
 import com.diagbot.client.DiagbotmanServiceClient;
 import com.diagbot.client.UserServiceClient;
+import com.diagbot.dto.RespDTO;
+import com.diagbot.exception.CommonErrorCode;
+import com.diagbot.exception.CommonException;
+import com.diagbot.exception.ErrorCode;
+import com.diagbot.util.GsonUtil;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.cloud.gateway.filter.GatewayFilterChain;
 import org.springframework.cloud.gateway.filter.GlobalFilter;
 import org.springframework.cloud.gateway.support.ServerWebExchangeUtils;
 import org.springframework.context.annotation.Configuration;
+import org.springframework.core.io.buffer.DataBuffer;
+import org.springframework.http.HttpStatus;
 import org.springframework.http.server.reactive.ServerHttpRequest;
 import org.springframework.web.server.ServerWebExchange;
+import reactor.core.publisher.Flux;
 import reactor.core.publisher.Mono;
 
 import java.net.URI;
+import java.nio.charset.StandardCharsets;
 import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.LinkedHashSet;
@@ -69,6 +78,10 @@ public class GlobalGatewayFilter implements GlobalFilter {
 //        }
 //        if(1 == 1) {
 //            throw new CommonException(CommonErrorCode.PARAM_IS_ERROR, "出错了!");
+//        }
+
+//        if(1==1){
+//            return getVoidMono(serverWebExchange, CommonErrorCode.NO_PERMISSION);
 //        }
 
         ServerHttpRequest.Builder builder = serverWebExchange.getRequest().mutate();
@@ -76,4 +89,20 @@ public class GlobalGatewayFilter implements GlobalFilter {
         gatewayFilterChain.filter(serverWebExchange.mutate().request(builder.build()).build());
         return gatewayFilterChain.filter(serverWebExchange.mutate().request(builder.build()).build());
     }
+
+    /**
+     * 网关抛异常
+     *
+     * @param body
+     */
+    private Mono<Void> getVoidMono(ServerWebExchange serverWebExchange, ErrorCode body) {
+        RespDTO resp = new RespDTO();
+        serverWebExchange.getResponse().setStatusCode(HttpStatus.OK);
+        resp.msg = body.getMsg();
+        resp.code = body.getCode();
+        resp.data = "";
+        byte[] bytes = GsonUtil.toJson(resp).getBytes(StandardCharsets.UTF_8);
+        DataBuffer buffer = serverWebExchange.getResponse().bufferFactory().wrap(bytes);
+        return serverWebExchange.getResponse().writeWith(Flux.just(buffer));
+    }
 }