整合营销服务商

电脑端+手机端+微信端=数据同步管理

免费咨询热线:

Android中的Handler, MessageQ

Android中的Handler, MessageQueu, Message, Looper

. ThreadLocal

在研究Handler的机制之前我们想明确一个知识点,ThreadLocal. ThreadLocal字面意思是线程本地变量,他保存的是一个变量的副本,并且只对当前线程可见。我们做个例子:

结果如下:

可以看到第一个和第二个打印的日志是一样的,也可能你觉得这个不能说明啥,一会我们看看源码也许你就明白了。

OK那我们看看他的源码:

在set值得时候,拿到当前的线程,并拿到一个map,就这个值存放到map中。这个TheaLocalMap是什么呢?

就一句话,还是从当前的线程里面去拿的,继续看下threadLocals:

那么这个ThreadLocalMap 是一个什么东西呢,继续往下看:

可以看到这个ThreadLocalMap中有一个存储结构用来存储键值对,这个value其实就是我们set进去的变量,继续看下set方法:

这个方法也不复杂,就是看看当前的数组table中是不是已经存过了,如果已经存过了那么久覆盖掉之前的Value,没有的话就创建一个Entry放到数组中。所以同一个ThreadLocal在一个线程中只能存放一个值,不然会被覆盖掉。

当然拿到当前的值时候也不难,明白了这个get方法也就很简单了。

总结下 ThreadLocal的作用 当前线程是绑定一个变量,只能当前线程访问,别的线程访问不了也修改不了他的值。在多线程的环境中, 当多个线程需要对某一个变量进行频繁的操作,但又不需要同步的时候,可以用ThreadLocal,因为他存储在当前的线程中的,效率恨到,其实就是空间换时间。

2. Handler Looper, MessageQueue, Message

Handler Looper MessageQueue Message他们之间关系其实不难理解,我们new一个Handler并实现里面的handMessage方法,参数是一个message, 然后用Handler发送message,发送的message到了MessageQuueue这个队列里面,Looper维持一个死循环,一直从MessageQueue里面取Message,之后调用相应的Handler里面的handMessage方法,这样就完成了一个循环。 说起来你不难,但是要理解里面的机制,还需要看源码:

首先一个应用在启动的时候,系统会调用ActivityThread.java中的main方法,在main方法里面会去初始化Looper。

可以看到调用了Looper.prepareMainLooper();这个方法其实就是初始化了一个Looper,这个Looper是给主线程调用,接着下面调用了Looper.loop() 这个方法是开启一个死循环,一会再看,先看下Looper.prepareMainLooper()

在上面的源码prepare中new了一个Looper并把它放到了ThreadLocal中,上面我们已经说了,ThreadLocal存储的变量和线程绑定在一起,那么当前的线程其实是主线程。 sMainLooper 这变量其实就是我们常用的Looper.getMainLooper().

那么MessageQueue是什么时候创建的,其实就是new Looper的时候创建的。

我们平常用Handler的时候都是new Handler 如下:

那么Handler发送的的message是怎么到队列里面,并且之后是怎么回调的,继续看源码:

所有的send方法都会掉到上面这个方法里面来,然后插进队列:

这个时候要注意上面的那个msg.target=this;这一句,可以看到message将会绑定当前发送的Handler,这也就为以后出来message埋下了伏笔,其实说白了就是拿到这个target指向的Handler来调用handmessage方法处理message。

那你有没有注意到那个mQueue 是从哪里冒出来的?

其实就是在new Handler的时候初始化的,可以看到上面Looper.myLooper();这句话,如果你有心的话其实上面已经有这个方法的实现了:

其实就拿的当前线程里面的Looper,因为这个是在主线程里面的new的所以其实就是拿到ActivityThread里面初始化的Looper。

既然已经拿到messageQueue并且将message放到队列里面的那么接下来是怎么出队列进行处理的呢,那就是我们的一开始就说的Looper.loop()了:

看上面的代码,我删除了一些东西,最主要的是就拿到messageQueue,并有一个死循环,不断的从messageQueue里面拿去message,接着调用msg.target.dispatchMessage(msg); 在直接发送message的时候,我们看过这个target其实就是绑定的当前的Handler,在dispatchMessage这个方法中就会去调用handMessage这个方法,到这里,整个流程就走完了。

关于死循环的问题,可以看看https://www.zhihu.com/question/34652589/answer/90344494 可以看看这个链接,牵扯到了Linux系统管道的问题了。

通过上面的分析我们也就知道了在主线程中可以newHandler 而在子线程中new Hander 必须自己手动调用Looper的原因了,因为主线程已经帮我实现好了

参考文章: https://www.cnblogs.com/alex-mh/p/6761233.html; https://www.cnblogs.com/alex-mh/p/6761233.html


截器Interceptor,是SpringMVC中的核心内容,利用spring的AOP(Aspect Oriented Programming, 面向切面编程)特性,可以很方便的对用户的业务代码进行横向抽取,根据具体业务需求对应用功能进行增强。
在SpringBoot中使用Interceptor,同时采用全注解开发,涉及到以下接口和类:

  1. HandlerInterceptor:处理器拦截器,handler就是处理器,在springboot web开发中,由控制器来处理web请求,因此handler具体指控制器
  2. 使用全注解开发,通过@Configuration注解,让一个java对象主任到IOC容器,并作为配置对象,这里的JavaConfig类相当于一个xml配置文件;
  3. 在以前的xml配置中
    (1)通过引入一些标签进行配置,在JavaConfig中,通过继承一个类或者实现一个接口来实现配置,这里所继承的类、所实现的接口就相当于引入的标签;
    (3)通过设置所引入标签的属性和值,可以实现个性化配置,在JavaConfig中通过覆盖类或者接口的方法来实现个性化配置。

下面通过一个案例来实现自定义拦截器
拦截/user/开头的请求,不拦截/usr/login请求

1. 定义拦截器

1234567891011121314package cn.eis220.web;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("执行了logininterceptor的preHandle方法");
        return true;
    }
}

2. 使用JavaConfig注册拦截器

java配置类相当于xml配置文件
xml中通过引入interceptor标签来进行配置,java配置类通过实现WebMvcController进行配置;
xml中通过修改标签的属性和值来个性化配置,java配置类通过实现WebMvcController的方法进行个性化配置

123456789101112131415161718package cn.eis220.config;

import cn.eis220.web.LoginInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class MyAppCofnig implements WebMvcConfigurer {
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        LoginInterceptor loginInterceptor=new LoginInterceptor();

        String[] path={"/user/**"};
        String[] excludePath={"/user/login"};
	registry.addInterceptor(loginInterceptor).addPathPatterns(path).excludePathPatterns(excludePath);
    }
}

3. 定义控制器,测试拦截器

123456789101112131415161718192021package cn.eis220.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class BootController {
    @RequestMapping("/user/account")
    @ResponseBody
    public String userAccount(){
        return "/user/account";
    }

    @RequestMapping("/user/login")
    @ResponseBody
    public String userLogin(){
        return "/user/login";
    }
}

4. 总结

步骤:

  1. 如何定义自己的拦截器:
    实现HandlerInterceptor接口的方法,来自定义拦截器
  2. 如何注册拦截器:
    JavaConfig配置类实现WebMvcConfigurer接口的addInterceptor方法来注册拦截器

本文作者:流云的博客

本文链接:https://www.cnblogs.com/arminker/p/15806725.html

时开发过程中,无可避免我们需要处理各类异常,所以这里我们在公共模块中自定义统一异常,Spring Boot 提供 @RestControllerAdvice 注解统一异常处理,我们在GitEgg_Platform中新建gitegg-platform-boot子工程,此工程主要用于Spring Boot相关功能的自定义及扩展。

1、修改gitegg-platform-boot的pom.xml,添加spring-boot-starter-web和swagger依赖,设置optional为true,让这个包在项目之间依赖不传递。

<dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-web</artifactId>
       <optional>true</optional>
</dependency>
<dependency>
       <groupId>com.gitegg.platform</groupId>
       <artifactId>gitegg-platform-swagger</artifactId>
       <optional>true</optional>
</dependency>

2、自定义通用响应消息类,Result和PageResult,一个是普通响应消息,一个是分页响应消息。

Result类:

package com.gitegg.platform.boot.common.base;
import com.gitegg.platform.boot.common.enums.ResultCodeEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;
/**
 * @ClassName: Result
 * @Description: 自定义通用响应类
 * @author GitEgg
 * @date 2020年09月19日 下午9:24:50
 */
@ApiModel(description="通用响应类")
@Getter
@ToString
public class Result<T> {
    @ApiModelProperty(value="是否成功", required=true)
    private boolean success;
    
    @ApiModelProperty(value="响应代码", required=true)
    private int code;
    @ApiModelProperty(value="提示信息", required=true)
    private String msg;
    @ApiModelProperty(value="响应数据")
    private T data;
    
    /**
     * 
     * @param code
     * @param data
     * @param msg
     */
    private Result(int code, T data, String msg) {
        this.success=ResultCodeEnum.SUCCESS.code==code;
        this.code=code;
        this.msg=msg;
        this.data=data;
    }
    /**
     * 
     * @param resultCodeEnum
     */
    private Result(ResultCodeEnum resultCodeEnum ) {
        this(resultCodeEnum.code, null, resultCodeEnum.msg);
    }
    /**
     * 
     * @param resultCodeEnum
     * @param msg
     */
    private Result(ResultCodeEnum resultCodeEnum , String msg) {
        this(resultCodeEnum, null, msg);
    }
    /**
     * 
     * @param resultCodeEnum
     * @param data
     */
    private Result(ResultCodeEnum resultCodeEnum , T data) {
        this(resultCodeEnum, data, resultCodeEnum.msg);
    }
    /**
     * 
     * @param resultCodeEnum
     * @param data
     * @param msg
     */
    private Result(ResultCodeEnum resultCodeEnum , T data, String msg) {
        this(resultCodeEnum.code, data, msg);
    }
    /**
     * 
     *
     * @param data 数据
     * @param <T>  T 响应数据
     * @
     */
    public static <T> Result<T> data(T data) {
        return data(data, ResultCodeEnum.SUCCESS.msg);
    }
    /**
     * 
     *
     * @param data 数据
     * @param msg  消息
     * @param <T>  T 响应数据
     * @
     */
    public static <T> Result<T> data(T data, String msg) {
        return data(ResultCodeEnum.SUCCESS.code, data, msg);
    }
    /**
     * 
     *
     * @param code 状态码
     * @param data 数据
     * @param msg  消息
     * @param <T>  T 响应数据
     * @
     */
    public static <T> Result<T> data(int code, T data, String msg) {
        return new Result<>(code, data, msg);
    }
    /**
     * 返回Result
     *
     * @param 
     * @param <T>  T 响应数据
     * @返回Result
     */
    public static <T> Result<T> success() {
        return new Result<>(ResultCodeEnum.SUCCESS);
    }
    
    /**
     * 返回Result
     *
     * @param msg 消息
     * @param <T> T 响应数据
     * @返回Result
     */
    public static <T> Result<T> success(String msg) {
        return new Result<>(ResultCodeEnum.SUCCESS, msg);
    }
    /**
     * 返回Result
     *
     * @param 
     * @param <T>  T 响应数据
     * @返回Result
     */
    public static <T> Result<T> success(ResultCodeEnum resultCodeEnum ) {
        return new Result<>(resultCodeEnum);
    }
    /**
     * 返回Result
     *
     * @param 
     * @param msg   提示信息
     * @param <T>  T 响应数据
     * @返回Result
     */
    public static <T> Result<T> success(ResultCodeEnum resultCodeEnum , String msg) {
        return new Result<>(resultCodeEnum, msg);
    }
    
    /**
     * 返回Result
     *
     * @param <T> T 响应数据
     * @返回Result
     */
    public static <T> Result<T> error() {
        return new Result<>(ResultCodeEnum.ERROR, ResultCodeEnum.ERROR.msg);
    }
    /**
     * 返回Result
     *
     * @param msg 消息
     * @param <T> T 响应数据
     * @返回Result
     */
    public static <T> Result<T> error(String msg) {
        return new Result<>(ResultCodeEnum.ERROR, msg);
    }
    /**
     * 返回Result
     *
     * @param code 状态码
     * @param msg  消息
     * @param <T>  T 响应数据
     * @返回Result
     */
    public static <T> Result<T> error(int code, String msg) {
        return new Result<>(code, null, msg);
    }
    /**
     * 返回Result
     *
     * @param 
     * @param <T>  T 响应数据
     * @返回Result
     */
    public static <T> Result<T> error(ResultCodeEnum resultCodeEnum ) {
        return new Result<>(resultCodeEnum);
    }
    /**
     * 返回Result
     *
     * @param 
     * @param msg   提示信息
     * @param <T>  T 响应数据
     * @返回Result
     */
    public static <T> Result<T> error(ResultCodeEnum resultCodeEnum , String msg) {
        return new Result<>(resultCodeEnum, msg);
    }
    
    /**
     * 
     * @param <T>
     * @param flag
     * @return
     */
    public static <T> Result<T> result(boolean flag) {
        return flag ? Result.success("操作成功") : Result.error("操作失败");
    }
}

PageResult类:

package com.gitegg.platform.boot.common.base;
import java.util.List;
import com.gitegg.platform.boot.common.enums.ResultCodeEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
/**
 * @ClassName: PageResult
 * @Description: 通用分页返回
 * @author GitEgg
 * @date
 * @param <T>
 */
@Data
@ApiModel("通用分页响应类")
public class PageResult<T> {
    @ApiModelProperty(value="是否成功", required=true)
    private boolean success;
    @ApiModelProperty(value="响应代码", required=true)
    private int code;
    @ApiModelProperty(value="提示信息", required=true)
    private String msg;
    @ApiModelProperty(value="总数量", required=true)
    private long count;
    @ApiModelProperty(value="分页数据")
    private List<T> data;
    public PageResult(long total, List<T> rows) {
        this.count=total;
        this.data=rows;
        this.code=ResultCodeEnum.SUCCESS.code;
        this.msg=ResultCodeEnum.SUCCESS.msg;
    }
}

3、自定义通用响应消息枚举类ResultCodeEnum。

package com.gitegg.platform.boot.common.enums;
/**
 * @ClassName: ResultCodeEnum
 * @Description: 自定义返回码枚举
 * @author GitEgg
 * @date 2020年09月19日 下午11:49:45
 */
public enum ResultCodeEnum {
    /**
     * 成功
     */
    SUCCESS(200, "操作成功"),
    /**
     * 系统错误
     */
    ERROR(500, "系统错误"),
    /**
     * 操作失败
     */
    FAILED(101, "操作失败"),
    /**
     * 未登录/登录超时
     */
    UNAUTHORIZED(102, "登录超时"),
    /**
     * 参数错误
     */
    PARAM_ERROR(103, "参数错误"),
    /**
     * 参数错误-已存在
     */
    INVALID_PARAM_EXIST(104, "请求参数已存在"),
    /**
     * 参数错误
     */
    INVALID_PARAM_EMPTY(105, "请求参数为空"),
    /**
     * 参数错误
     */
    PARAM_TYPE_MISMATCH(106, "参数类型不匹配"),
    /**
     * 参数错误
     */
    PARAM_VALID_ERROR(107, "参数校验失败"),
    /**
     * 参数错误
     */
    ILLEGAL_REQUEST(108, "非法请求"),
    /**
     * 验证码错误
     */
    INVALID_VCODE(204, "验证码错误"),
    /**
     * 用户名或密码错误
     */
    INVALID_USERNAME_PASSWORD(205, "账号或密码错误"),
    /**
     *
     */
    INVALID_RE_PASSWORD(206, "两次输入密码不一致"),
    /**
     * 用户名或密码错误
     */
    INVALID_OLD_PASSWORD(207, "旧密码错误"),
    /**
     * 用户名重复
     */
    USERNAME_ALREADY_IN(208, "用户名已存在"),
    /**
     * 用户不存在
     */
    INVALID_USERNAME(209, "用户名不存在"),
    /**
     * 角色不存在
     */
    INVALID_ROLE(210, "角色不存在"),
    /**
     * 角色不存在
     */
    ROLE_USED(211, "角色使用中,不可删除"),
    /**
     * 没有权限
     */
    NO_PERMISSION(403, "当前用户无该接口权限");
    public int code;
    public String msg;
    ResultCodeEnum(int code, String msg) {
        this.code=code;
        this.msg=msg;
    }
    public int getCode() {
        return code;
    }
    public void setCode(int code) {
        this.code=code;
    }
    public String getMsg() {
        return msg;
    }
    public void setMsg(String msg) {
        this.msg=msg;
    }
}

4、自定义异常类BusinessException和SystemException

package com.gitegg.platform.boot.common.exception;
import com.gitegg.platform.boot.common.enums.ResultCodeEnum;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
/**
 * @ClassName: BusinessException
 * @Description: 业务处理异常
 * @author GitEgg
 * @date
 */
@Getter
@Setter
public class BusinessException extends RuntimeException {
    private int code;
    private String msg;
    public BusinessException() {
        this.code=ResultCodeEnum.FAILED.code;
        this.msg=ResultCodeEnum.FAILED.msg;
    }
    public BusinessException(String message) {
        this.code=ResultCodeEnum.FAILED.code;
        this.msg=message;
    }
    public BusinessException(int code, String msg) {
        this.code=code;
        this.msg=msg;
    }
    public BusinessException(Throwable cause) {
        super(cause);
    }
    public BusinessException(String message, Throwable cause) {
        super(message, cause);
    }
}


package com.gitegg.platform.boot.common.exception;
import com.gitegg.platform.boot.common.enums.ResultCodeEnum;
import lombok.Getter;
/**
 * @ClassName: SystemException
 * @Description: 系统处理异常
 * @author GitEgg
 * @date
 */
@Getter
public class SystemException extends RuntimeException {
    private int code;
    private String msg;
    public SystemException() {
        this.code=ResultCodeEnum.ERROR.code;
        this.msg=ResultCodeEnum.ERROR.msg;
    }
    public SystemException(String message) {
        this.code=ResultCodeEnum.ERROR.code;
        this.msg=message;
    }
    public SystemException(int code, String msg) {
        this.code=code;
        this.msg=msg;
    }
    public SystemException(Throwable cause) {
        super(cause);
    }
    public SystemException(String message, Throwable cause) {
        super(message, cause);
    }
}

5、自定义统一异常处理类GitEggControllerAdvice.java

package com.gitegg.platform.boot.common.advice;
import com.gitegg.platform.boot.common.base.Result;
import com.gitegg.platform.boot.common.enums.ResultCodeEnum;
import com.gitegg.platform.boot.common.exception.BusinessException;
import com.gitegg.platform.boot.common.exception.SystemException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.ui.Model;
import org.springframework.web.HttpMediaTypeNotAcceptableException;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingPathVariableException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
import org.springframework.web.servlet.NoHandlerFoundException;
import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import javax.validation.ConstraintViolationException;
@Slf4j
@RestControllerAdvice
public class GitEggControllerAdvice {
    /**
     * 服务名
     */
    @Value("${spring.application.name}")
    private String serverName;
    /**
     * 微服务系统标识
     */
    private String errorSystem;
    @PostConstruct
    public void init() {
        this.errorSystem=new StringBuffer()
                .append(this.serverName)
                .append(": ").toString();
    }
    /**
     * 应用到所有@RequestMapping注解方法,在其执行之前初始化数据绑定器
     */
    @InitBinder
    public void initBinder(WebDataBinder binder) {
    }
    /**
     * 把值绑定到Model中,使全局@RequestMapping可以获取到该值
     */
    @ModelAttribute
    public void addAttributes(Model model) {
    }
    /**
     * 全局异常捕捉处理
     */
    @ExceptionHandler(value={Exception.class})
    public Result handlerException(Exception exception, HttpServletRequest request) {
        log.error("请求路径uri={},系统内部出现异常:{}", request.getRequestURI(), exception);
        Result result=Result.error(ResultCodeEnum.ERROR, errorSystem + exception.toString());
        return result;
    }
    /**
     * 非法请求异常
     */
    @ExceptionHandler(value={
            HttpMediaTypeNotAcceptableException.class,
            HttpMediaTypeNotSupportedException.class,
            HttpRequestMethodNotSupportedException.class,
            MissingServletRequestParameterException.class,
            NoHandlerFoundException.class,
            MissingPathVariableException.class,
            HttpMessageNotReadableException.class
    })
    public Result handlerSpringAOPException(Exception exception) {
        Result result=Result.error(ResultCodeEnum.ILLEGAL_REQUEST, errorSystem + exception.getMessage());
        return result;
    }
    /**
     * 非法请求异常-参数类型不匹配
     */
    @ExceptionHandler(value=MethodArgumentTypeMismatchException.class)
    public Result handlerSpringAOPException(MethodArgumentTypeMismatchException exception) {
        Result result=Result.error(ResultCodeEnum.PARAM_TYPE_MISMATCH, errorSystem + exception.getMessage());
        return result;
    }
    /**
     * 非法请求-参数校验
     */
    @ExceptionHandler(value={MethodArgumentNotValidException.class})
    public Result handlerMethodArgumentNotValidException(MethodArgumentNotValidException methodArgumentNotValidException) {
        //获取异常字段及对应的异常信息
        StringBuffer stringBuffer=new StringBuffer();
        methodArgumentNotValidException.getBindingResult().getFieldErrors().stream()
                .map(t -> t.getField()+"=>"+t.getDefaultMessage()+" ")
                .forEach(e -> stringBuffer.append(e));
        String errorMessage=stringBuffer.toString();
        Result result=Result.error(ResultCodeEnum.PARAM_VALID_ERROR, errorSystem + errorMessage);
        return result;
    }
    /**
     * 非法请求异常-参数校验
     */
    @ExceptionHandler(value={ConstraintViolationException.class})
    public Result handlerConstraintViolationException(ConstraintViolationException constraintViolationException) {
        String errorMessage=constraintViolationException.getLocalizedMessage();
        Result result=Result.error(ResultCodeEnum.PARAM_VALID_ERROR, errorSystem + errorMessage);
        return result;
    }
    /**
     * 自定义业务异常-BusinessException
     */
    @ExceptionHandler(value={BusinessException.class})
    public Result handlerCustomException(BusinessException exception) {
        String errorMessage=exception.getMsg();
        Result result=Result.error(exception.getCode(), errorSystem + errorMessage);
        return result;
    }
    /**
     * 自定义系统异常-SystemException
     */
    @ExceptionHandler(value={SystemException.class})
    public Result handlerCustomException(SystemException exception) {
        String errorMessage=exception.getMsg();
        Result result=Result.error(exception.getCode(), errorSystem + errorMessage);
        return result;
    }
}

6、重新将GitEgg-Platform进行install,在GitEgg-Cloud中的gitegg-service引入gitegg-platform-boot

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>GitEgg-Cloud</artifactId>
        <groupId>com.gitegg.cloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>gitegg-service</artifactId>
    <packaging>pom</packaging>
    <modules>
        <module>gitegg-service-base</module>
        <module>gitegg-service-bigdata</module>
        <module>gitegg-service-system</module>
    </modules>
    <dependencies>
        <!-- gitegg Spring Boot自定义及扩展 -->
        <dependency>
            <groupId>com.gitegg.platform</groupId>
            <artifactId>gitegg-platform-boot</artifactId>
        </dependency>
        <!-- gitegg数据库驱动及连接池 -->
        <dependency>
            <groupId>com.gitegg.platform</groupId>
            <artifactId>gitegg-platform-db</artifactId>
        </dependency>
        <!-- gitegg mybatis-plus -->
        <dependency>
            <groupId>com.gitegg.platform</groupId>
            <artifactId>gitegg-platform-mybatis</artifactId>
        </dependency>
        <!-- gitegg swagger2-knife4j -->
        <dependency>
            <groupId>com.gitegg.platform</groupId>
            <artifactId>gitegg-platform-swagger</artifactId>
        </dependency>
        <!-- spring boot web核心包 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- spring boot 健康监控 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
    </dependencies>
</project>

7、修改SystemController.java、ISystemService.java和SystemServiceImpl.java增加异常处理的测试代码

SystemController.java:

package com.gitegg.service.system.controller;
import com.gitegg.platform.boot.common.base.Result;
import com.gitegg.platform.boot.common.exception.BusinessException;
import com.gitegg.service.system.service.ISystemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping(value="system")
@AllArgsConstructor
@Api(tags="gitegg-system")
public class SystemController {
    private final ISystemService systemService;
    @GetMapping(value="list")
    @ApiOperation(value="system list接口")
    public Object list() {
        return systemService.list();
    }
    @GetMapping(value="page")
    @ApiOperation(value="system page接口")
    public Object page() {
        return systemService.page();
    }
    @GetMapping(value="exception")
    @ApiOperation(value="自定义异常及返回测试接口")
    public Result<String> exception() {
        return Result.data(systemService.exception());
    }
}

ISystemService.java:

package com.gitegg.service.system.service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gitegg.service.system.entity.SystemTable;
import java.util.List;
public interface ISystemService {
    List<SystemTable> list();
    Page<SystemTable> page();
    String exception();
}

SystemServiceImpl.java:

package com.gitegg.service.system.service.impl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gitegg.platform.boot.common.exception.BusinessException;
import com.gitegg.service.system.entity.SystemTable;
import com.gitegg.service.system.mapper.SystemTableMapper;
import com.gitegg.service.system.service.ISystemService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import java.util.List;
/**
 *
 */
@Service
@AllArgsConstructor
public class SystemServiceImpl implements ISystemService {
    private final SystemTableMapper systemTableMapper;
    @Override
    public List<SystemTable> list() {
        return systemTableMapper.list();
    }
    @Override
    public Page<SystemTable> page() {
        Page<SystemTable> page=new Page<>(1, 10);
        List<SystemTable> records=systemTableMapper.page(page);
        page.setRecords(records);
        return page;
    }
    @Override
    public String exception() {
        throw new BusinessException("自定义异常");
//        return "成功获得数据";
    }
}

8、运行GitEggSystemApplication,打开浏览器访问:http://127.0.0.1:8001/doc.html,然后点击左侧的异常处理接口,使用Swagger2进行测试,即可看到结果

源码在https://gitee.com/wmz1930/GitEgg 的chapter-07分支。