Offer 驾到,掘友接招!我正在参与2022春招打卡活动,点击查看活动详情。
工具类编写
在正式开发接口前,我们还需要配置一些工具类,例如接口返回的结果集该怎么配置,对于日期该怎么处理,发生的异常该怎么处理,跨域请求怎么办等等问题。
第一步:新建 Package
在我们的项目目录(com.qq.first_project)下新建一个 Package (common),以后我们可以将所有工具类代码都放在里面。
第二步:新建工具类代码
在该目录下新建如下工具类代码。
通用配置工具类 GlobalConfig
package com.qq.first_project.common;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.plugins.pagination.dialects.MySqlDialect;
import com.baomidou.mybatisplus.extension.plugins.pagination.optimize.JsqlParserCountOptimize;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.qq.first_project.common.DateConverter;
import com.qq.first_project.common.LocalDateTimeConverter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
/**
*
* @ClassName: GlobalConfig
* @Description: 通用配置
* @author: Qiu
*/
@Configuration
public class GlobalConfig extends WebMvcConfigurationSupport {
/**
* 分页插件
* @return
*/
@Bean
public PaginationInterceptor paginationInterceptor() {
PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
paginationInterceptor.setDialect(new MySqlDialect());
paginationInterceptor.setCountSqlParser(new JsqlParserCountOptimize(true));
return paginationInterceptor;
}
/**
* spring boot 跨域过滤器
* @return
*/
@Bean
FilterRegistrationBean<CorsFilter> cors(){
FilterRegistrationBean<CorsFilter> registrationBean = new FilterRegistrationBean<CorsFilter>();
CorsConfiguration config = new CorsConfiguration();
config.setAllowCredentials(true);
config.addAllowedOrigin("http://localhost:8120");
config.addAllowedHeader("*");
config.addAllowedMethod("*");
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", config);
CorsFilter filter = new CorsFilter(source);
registrationBean.setFilter(filter); //注册自定义过滤器
registrationBean.addUrlPatterns("/*"); //过滤所有路径
return registrationBean;
}
/**
* springboot自定义消息转换器
* curl http://127.0.0.1/user/getById?id=31
* @param converters
*/
@Override
protected void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
objectMapper.disable(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE);
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); //忽略 null 字段
JavaTimeModule javaTimeModule = new JavaTimeModule();
javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
javaTimeModule.addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
objectMapper.registerModule(javaTimeModule);
converters.add(new MappingJackson2HttpMessageConverter(objectMapper));
}
/**
* 数据格式化
* @param registry
*/
@Override
protected void addFormatters(FormatterRegistry registry) {
super.addFormatters(registry);
registry.addConverter(new DateConverter());
registry.addConverter(new LocalDateTimeConverter());
}
}
异常处理工具类 DefaultExceptionHandler
package com.qq.first_project.common;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.jdbc.BadSqlGrammarException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
/**
*
* @ClassName: DefaultExceptionHandler
* @Description:异常处理
* @author: Qiu
*/
@Slf4j
@ControllerAdvice
public class DefaultExceptionHandler {
@ExceptionHandler(Exception.class)
@ResponseBody
public R error(Exception e){
e.printStackTrace();
return R.error();
}
@ExceptionHandler(BadSqlGrammarException.class)
@ResponseBody
public R error(BadSqlGrammarException e){
e.printStackTrace();
return R.setResult(ResultCode.BAD_SQL_GRAMMAR);
}
@ExceptionHandler(HttpMessageNotReadableException.class)
@ResponseBody
public R error(HttpMessageNotReadableException e){
e.printStackTrace();
return R.setResult(ResultCode.JSON_PARSE_ERROR);
}
}
日期处理工具类 DateConverter 和 LocalDateTimeConverter
DateConverter
package com.qq.first_project.common;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.converter.Converter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Pattern;
/**
*
* @ClassName: DateConverter
* @Description: 日期处理
* @author: Qiu
*/
@Configuration
public class DateConverter implements Converter<String, Date> {
public DateConverter() {
}
static List<SimpleDateFormat> sdfs = new ArrayList();
static {
sdfs.add(new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"));
sdfs.add(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
sdfs.add(new SimpleDateFormat("yyyy-MM-dd"));
sdfs.add(new SimpleDateFormat("yyyy/MM/dd"));
}
@Override
public Date convert(String s) {
SimpleDateFormat sdf = null;
if (Pattern.matches("^\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}$", s)) {
sdf = sdfs.get(1);
}else if (Pattern.matches("^\\d{4}/\\d{2}/\\d{2} \\d{2}:\\d{2}:\\d{2}$", s)){
sdf = sdfs.get(0);
}else if (Pattern.matches("^\\d{4}/\\d{2}/\\d{2}$", s)){
sdf = sdfs.get(3);
}else if (Pattern.matches("^\\d{4}-\\d{2}/-\\d{2}}$", s)){
sdf = sdfs.get(2);
}
Date date = null;
try {
date = sdf.parse(s);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
}
LocalDateTimeConverter
package com.qq.first_project.common;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
/**
*
* @ClassName: LocalDateTimeConverter
* @Description: 日期处理 2022-03-09 11:02:03
* @author: Qiu
*/
@Component
public class LocalDateTimeConverter implements Converter<String, LocalDateTime> {
@Override
public LocalDateTime convert(String s) {
return LocalDateTime.parse(s, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
}
}
结果集工具类 ResultCode 和 R
ResultCode
package com.qq.first_project.common;
import lombok.Getter;
import lombok.ToString;
/**
*
* @ClassName: ResultCode
* @Description: 返回码
* @author: Qiu
*/
@Getter
@ToString
public enum ResultCode {
SUCCESS(true, 20000,"成功"),
UNKNOWN_REASON(false, 20001, "未知错误"),
BAD_SQL_GRAMMAR(false, 21001, "sql语法错误"),
JSON_PARSE_ERROR(false, 21002, "json解析异常"),
PARAM_ERROR(false, 21003, "参数不正确"),
FILE_UPLOAD_ERROR(false, 21004, "文件上传错误"),
FILE_DELETE_ERROR(false, 21005, "文件刪除错误"),
EXCEL_DATA_IMPORT_ERROR(false, 21006, "Excel数据导入错误"),
VIDEO_UPLOAD_ALIYUN_ERROR(false, 22001, "视频上传至阿里云失败"),
VIDEO_UPLOAD_TOMCAT_ERROR(false, 22002, "视频上传至业务服务器失败"),
VIDEO_DELETE_ALIYUN_ERROR(false, 22003, "阿里云视频文件删除失败"),
FETCH_VIDEO_UPLOADAUTH_ERROR(false, 22004, "获取上传地址和凭证失败"),
REFRESH_VIDEO_UPLOADAUTH_ERROR(false, 22005, "刷新上传地址和凭证失败"),
FETCH_PLAYAUTH_ERROR(false, 22006, "获取播放凭证失败"),
URL_ENCODE_ERROR(false, 23001, "URL编码失败"),
ILLEGAL_CALLBACK_REQUEST_ERROR(false, 23002, "非法回调请求"),
FETCH_ACCESSTOKEN_FAILD(false, 23003, "获取accessToken失败"),
FETCH_USERINFO_ERROR(false, 23004, "获取用户信息失败"),
LOGIN_ERROR(false, 23005, "登录失败"),
COMMENT_EMPTY(false, 24006, "评论内容必须填写"),
PAY_RUN(false, 25000, "支付中"),
PAY_UNIFIEDORDER_ERROR(false, 25001, "统一下单错误"),
PAY_ORDERQUERY_ERROR(false, 25002, "查询支付结果错误"),
ORDER_EXIST_ERROR(false, 25003, "课程已购买"),
GATEWAY_ERROR(false, 26000, "服务不能访问"),
CODE_ERROR(false, 28000, "验证码错误"),
LOGIN_PHONE_ERROR(false, 28009, "手机号码不正确"),
LOGIN_MOBILE_ERROR(false, 28001, "账号不正确"),
LOGIN_PASSWORD_ERROR(false, 28008, "密码不正确"),
LOGIN_DISABLED_ERROR(false, 28002, "该用户已被禁用"),
REGISTER_MOBLE_ERROR(false, 28003, "手机号已被注册"),
LOGIN_AUTH(false, 28004, "需要登录"),
LOGIN_ACL(false, 28005, "没有权限"),
SMS_SEND_ERROR(false, 28006, "短信发送失败"),
SMS_SEND_ERROR_BUSINESS_LIMIT_CONTROL(false, 28007, "短信发送过于频繁");
private Boolean success;
private Integer code;
private String message;
ResultCode(Boolean success, Integer code, String message) {
this.success = success;
this.code = code;
this.message = message;
}
}
R
package com.qq.first_project.common;
import lombok.Data;
import java.util.HashMap;
import java.util.Map;
/**
*
* @ClassName: R
* @Description: 返回结果集
* @author: Qiu
*/
@Data
public class R {
/**
* 是否成功
*/
private Boolean success;
/**
* 返回码"
*/
private Integer code;
/**
* 返回消息
*/
private String message;
/**
* 返回数据
*/
private Map<String, Object> data = new HashMap<String, Object>();
public R(){}
public static R ok(){
R r = new R();
r.setSuccess(ResultCode.SUCCESS.getSuccess());
r.setCode(ResultCode.SUCCESS.getCode());
r.setMessage(ResultCode.SUCCESS.getMessage());
return r;
}
public static R error(){
R r = new R();
r.setSuccess(ResultCode.UNKNOWN_REASON.getSuccess());
r.setCode(ResultCode.UNKNOWN_REASON.getCode());
r.setMessage(ResultCode.UNKNOWN_REASON.getMessage());
return r;
}
public static R setResult(ResultCode resultCode){
R r = new R();
r.setSuccess(resultCode.getSuccess());
r.setCode(resultCode.getCode());
r.setMessage(resultCode.getMessage());
return r;
}
public R success(Boolean success){
this.setSuccess(success);
return this;
}
public R message(String message){
this.setMessage(message);
return this;
}
public R code(Integer code){
this.setCode(code);
return this;
}
public R data(String key, Object value){
this.data.put(key, value);
return this;
}
public R data(Map<String, Object> map){
this.setData(map);
return this;
}
}
后续可能还会有更多工具类,配置等等,可根据项目情况再增加。