Browse Source

异常类提交

chenweida 7 years ago
parent
commit
37a65d1ca3

+ 26 - 0
common/common-rest-model/src/main/java/com/yihu/jw/restmodel/exception/SecurityException.java

@ -0,0 +1,26 @@
package com.yihu.jw.restmodel.exception;
/**
 * Created by chenweida on 2017/6/16.
 */
public class SecurityException extends Exception{
    public SecurityException() {
        super();
    }
    public SecurityException(String message) {
        super(message);
    }
    public SecurityException(String message, Throwable cause) {
        super(message, cause);
    }
    public SecurityException(Throwable cause) {
        super(cause);
    }
    protected SecurityException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
        super(message, cause, enableSuppression, writableStackTrace);
    }
}

+ 26 - 0
common/common-rest-model/src/main/java/com/yihu/jw/restmodel/exception/SystemException.java

@ -0,0 +1,26 @@
package com.yihu.jw.restmodel.exception;
/**
 * Created by chenweida on 2017/6/16.
 */
public class SystemException extends Exception{
    public SystemException() {
    }
    public SystemException(String message) {
        super(message);
    }
    public SystemException(String message, Throwable cause) {
        super(message, cause);
    }
    public SystemException(Throwable cause) {
        super(cause);
    }
    public SystemException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
        super(message, cause, enableSuppression, writableStackTrace);
    }
}

+ 1 - 1
common/common-rest-model/src/main/java/com/yihu/jw/restmodel/exception/ManageException.java

@ -1,4 +1,4 @@
package com.yihu.jw.restmodel.exception;
package com.yihu.jw.restmodel.exception.business;
/**
 * Created by chenweida on 2017/6/9.

+ 0 - 2
svr/svr-manage/src/main/java/com/yihu/jw/manage/interceptors/UserInterceptor.java

@ -3,7 +3,6 @@ package com.yihu.jw.manage.interceptors;
import com.yihu.jw.manage.model.system.ManageUser;
import com.yihu.jw.manage.service.system.UserService;
import com.yihu.jw.restmodel.common.Envelop;
import com.yihu.jw.restmodel.exception.ManageException;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@ -14,7 +13,6 @@ import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**

+ 1 - 1
svr/svr-manage/src/main/java/com/yihu/jw/manage/service/login/LoginService.java

@ -10,7 +10,7 @@ import com.yihu.jw.manage.model.system.MenuItems;
import com.yihu.jw.manage.service.system.MenuService;
import com.yihu.jw.manage.service.system.RoleService;
import com.yihu.jw.manage.service.system.UserService;
import com.yihu.jw.restmodel.exception.ManageException;
import com.yihu.jw.restmodel.exception.business.ManageException;
import com.yihu.jw.restmodel.manage.system.ManageUserVO;
import com.yihu.jw.util.security.MD5;
import org.springframework.beans.BeanUtils;

+ 1 - 1
svr/svr-manage/src/main/java/com/yihu/jw/manage/service/system/UserService.java

@ -3,7 +3,7 @@ package com.yihu.jw.manage.service.system;
import com.yihu.jw.manage.dao.system.UserDao;
import com.yihu.jw.manage.dao.system.UserRoleDao;
import com.yihu.jw.manage.model.system.ManageUser;
import com.yihu.jw.restmodel.exception.ManageException;
import com.yihu.jw.restmodel.exception.business.ManageException;
import com.yihu.jw.restmodel.wlyy.WlyyContant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;

+ 67 - 0
web-gateway/src/main/java/com/yihu/jw/config/mvc/GlobalHandlerExceptionResolver.java

@ -0,0 +1,67 @@
package com.yihu.jw.config.mvc;
import com.yihu.jw.restmodel.exception.SecurityException;
import com.yihu.jw.restmodel.exception.SystemException;
import com.yihu.jw.restmodel.exception.business.ManageException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
/**
 * Created by chenweida on 20170317.
 * 全局异常处理
 */
@Component
public class GlobalHandlerExceptionResolver implements HandlerExceptionResolver {
    private Logger logger = LoggerFactory.getLogger(GlobalHandlerExceptionResolver.class);
    private static Integer status_500 = 500;//后台异常
    private static Integer status_510 = 510;//后台管理系统异常
    private static Integer status_403 = 403;//没权限 未登录 等权限异常
    /**
     * 在这里处理所有得异常信息
     */
    @Override
    public ModelAndView resolveException(HttpServletRequest req, HttpServletResponse resp, Object o, Exception ex) {
        String error = ex.getMessage();
        logger.error(error);
        if (ex instanceof ManageException) {
            //后台管理系统异常
            printWrite(status_510, error, resp);
        } else if (ex instanceof SecurityException) {
            //权限异常
            printWrite(status_403, error, resp);
        } else {
            //系统异常
            printWrite(status_500, error, resp);
        }
        return new ModelAndView();
    }
    /**
     * 将错误信息添加到response中
     */
    public static void printWrite(int status, String msg, HttpServletResponse response) {
        try {
            response.setStatus(status);
            response.setCharacterEncoding("UTF-8");//设置编码
            response.setHeader("Cache-Control", "no-store");
            response.setHeader("Pragma", "no-cache");
            response.setHeader("Content-Type", "Content-Type: text/html; charset=utf-8");
            response.setHeader("Accept Encoding", "utf-8");
            PrintWriter pw = response.getWriter();
            pw.write(msg);
            pw.flush();
            pw.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

+ 6 - 1
web-gateway/src/main/java/com/yihu/jw/config/MvcConfig.java

@ -1,4 +1,4 @@
package com.yihu.jw.config;
package com.yihu.jw.config.mvc;
import com.yihu.jw.version.JWRequestMappingHandlerMapping;
import org.springframework.context.annotation.Bean;
@ -20,4 +20,9 @@ public class MvcConfig extends WebMvcConfigurationSupport {
        handlerMapping.setInterceptors(getInterceptors());
        return handlerMapping;
    }
    @Bean
    public GlobalHandlerExceptionResolver globalHandlerExceptionResolver() {
        return new GlobalHandlerExceptionResolver();
    }
}

+ 16 - 8
web-gateway/src/main/java/com/yihu/jw/controller/PatientController.java

@ -3,6 +3,9 @@ package com.yihu.jw.controller;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.yihu.jw.fegin.PatientFegin;
import com.yihu.jw.restmodel.exception.SystemException;
import com.yihu.jw.restmodel.exception.SecurityException;
import com.yihu.jw.restmodel.exception.business.ManageException;
import com.yihu.jw.version.ApiVersion;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
@ -10,13 +13,8 @@ import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.cloud.sleuth.Tracer;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.context.annotation.SessionScope;
import javax.servlet.http.HttpServletRequest;
@ -37,8 +35,18 @@ public class PatientController {
    @GetMapping("/hello")
    @ApiVersion(1)
    @ResponseBody
    public String hello1(HttpServletRequest request) {
        System.out.println("haha1..........");
    public String hello1(Integer id) throws Exception {
        switch (id){
            case 1:{
                throw new ManageException("后台管理系统异常");
            }
            case 2:{
                throw new SecurityException("权限异常");
            }
            case 3:{
                throw new SystemException("后台系统异常");
            }
        }
        return "hello1";
    }
@ -46,7 +54,7 @@ public class PatientController {
    @GetMapping("/hello")
    @ApiVersion(2)
    @ResponseBody
    public String hello2(HttpServletRequest request) {
    public String hello2(HttpServletRequest request) throws Exception {
        System.out.println("haha2.........");
        return "hello2";
    }

+ 24 - 0
web-gateway/src/main/java/com/yihu/jw/controller/login/LoginController.java

@ -0,0 +1,24 @@
package com.yihu.jw.controller.login;
import com.yihu.jw.commnon.wlyy.PatientContants;
import com.yihu.jw.restmodel.common.Envelop;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
/**
 * Created by chenweida on 2017/6/16.
 */
@RestController
@Api(value = "登陆相关操作", description = "登陆相关操作")
public class LoginController {
    @GetMapping(value = "/login", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiOperation(value = "登陆", notes = "登陆")
    public Envelop create(
            @ApiParam(name = "account", value = "", defaultValue = "") @RequestParam(name = "account", required = true) String account) {
        return null;
    }
}

+ 2 - 2
web-gateway/src/main/resources/application.yml

@ -19,8 +19,8 @@ management:
zuul:
  ignored-services: '*'  ##忽略全部的代理  忽略单个微服务   ignored-services: svr-base 多个逗号分割
  routes:
    svr-base: /base/**  ##svr-base方向代理到/base下多层级的路径
    svr-wlyy: /wlyy/**
    svr-base: /v1/base/**  ##svr-base方向代理到/base下多层级的路径
    svr-wlyy: /v1/wlyy/**
#    svr-base:    这种方式和  svr-base: /base/**  一样  svr-base可以随便写 唯一即可
#      path: /base/**  path是代理后的路径
#      serviceId: svr-base  serviceId是微服务name