【springboot】功能合集

目录

  • 全局监听请求:HandlerInterceptor
    • 创建拦截器类
    • 添加拦截器
    • 拦截器类调用Service服务
  • 全局异常处理:@ExceptionHandler
    • 统一处理业务异常
    • 自定义JsonResult
  • 全局跨域配置:WebMvcConfigurer
  • 静态(static)方法中调用接口(Service层)
  • 定时任务

全局监听请求:HandlerInterceptor

创建拦截器类

public class RequestInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        return HandlerInterceptor.super.preHandle(request, response, handler);
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        ...
        TrackUtil.report(info); // 通过工具类调用服务
    }
}

添加拦截器

@Configuration
public class WebConfig implements WebMvcConfigurer {
    @Override
    public void addInterceptors(InterceptorRegistry interceptorRegistry) {
        interceptorRegistry.addInterceptor(...); // 其他拦截器
        interceptorRegistry.addInterceptor(new RequestInterceptor()); // 添加本拦截器
    }
}

拦截器类调用Service服务

借助工具类

@Component
public class TrackUtil {
    
    public static TrackUtil trackUtil; //声明对象
    
    @Autowired //注入
    TrackService trackService;

    @PostConstruct //初始化
    public void init() {
        trackUtil = this;
        trackUtil.trackService = this.trackService;
    }


    public static void report(Object info){
        trackUtil.trackService.report(info);
    }
}

全局异常处理:@ExceptionHandler

统一处理业务异常

@ControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler({ServletException.class})
    @ResponseBody
    @ResponseStatus(value = HttpStatus.BAD_REQUEST)
    public JsonResult errorHandler1(HttpServletRequest request,
                                   HttpServletResponse response,
                                   Exception e) {
        e.printStackTrace();
        response.setCharacterEncoding("UTF-8");//防止返回中文乱码
        return JsonResult.build(400, e.getMessage());
    }

    @ExceptionHandler({Exception.class})
    @ResponseBody
    @ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
    public JsonResult errorHandler(HttpServletRequest request,
                                   HttpServletResponse response,
                                   Exception e) {
        e.printStackTrace();
        response.setCharacterEncoding("UTF-8");//防止返回中文乱码
        return JsonResult.build(500, e.getMessage());
    }
}

自定义JsonResult

@Data
public class JsonResult {
    private Integer code; //响应状态码
    private String msg; //响应消息
    private Object data; //响应数据

    public JsonResult(Integer code, String msg, Object data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public JsonResult(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public static JsonResult build(Integer status, String msg, Object data) {
        JsonResult jsonResult = new JsonResult(status, msg, data);
        return jsonResult;
    }

    public static JsonResult build(Integer status, String msg) {
        JsonResult jsonResult = new JsonResult(status, msg);
        return jsonResult;
    }

    public static JsonResult ok( Object data) {
        JsonResult jsonResult = new JsonResult(200, "success", data);
        return jsonResult;
    }

    @Override
    public String toString() {
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("code",code);
        jsonObject.put("msg",msg);
        jsonObject.put("data",data);

        return JSON.toJSONString(jsonObject);
    }
}

全局跨域配置:WebMvcConfigurer

@Configuration
public class WebConfig implements WebMvcConfigurer {
    @Override
    public void addCorsMappings(CorsRegistry corsRegistry) {
        corsRegistry.addMapping("/**")
                .allowedOrigins("*")
                .allowedMethods("GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS")
                .allowCredentials(true)
                .maxAge(3600)
                .allowedHeaders("*");
    }
}

静态(static)方法中调用接口(Service层)

@Component
public class JwtUtil {
    /**
     * 静态方法调用非静态接口层(Service层)
     */
    public static JwtUtil jwtUtil; //声明对象

    @PostConstruct //初始化
    public void init() {
        jwtUtil = this;
        jwtUtil.userService = this.userService;
    }

    @Autowired //注入
    UserService userService;


public static boolean checkSign(String token) {
...
 String username= jwtUtil.userService.getUsernameById(userId);
...
}

定时任务

package com.example.demo.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Component
@EnableScheduling //开启定时任务
@EnableAsync //开启多线程
@Slf4j
public class ScheduleAsync {
    /** cron
     * [秒] [分] [小时] [日] [月] [周] [年]
     * 年非必须
     * *每秒/分/小时/日...都会触发
     * ?不指定值,只可用于日和周
     * 1-3表示区间,1、2、3都会触发
     * 5/15表示递增触发,从5开始,每15秒触发
     * L表示最后一天或周六
    */
    @Async
    @Scheduled(cron = "0/10 * * * * ?")//从00开始每十秒
    public void first(){
        log.info(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
    }

    @Async
    @Scheduled(cron = "0 0 2 * * ?")//每天半夜两点
    public void second(){
        log.info(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
    }
}

你可能感兴趣的:(后端,spring,boot,java,后端)