Macula Boot Starter Web

概览

该模块为 Web 开发所需的基本依赖包,是对 spring-boot-starter-web 的扩展。Macula Boot 6.1 随 Spring Boot 4 使用其默认的 Tomcat Web 服务器;Spring Boot 4 不再提供 Undertow Starter。有兴趣可以阅读优雅的写Controller。

组件坐标


<dependency>
   <groupId>dev.macula.boot</groupId>
   <artifactId>macula-boot-starter-web</artifactId>
   <version>${macula.version}</version>
</dependency> 

使用配置

macula:
  web:
    exception-advice: true  # 统一异常处理器,默认true
    response-advice: true   # 统一响应处理器,默认true
属性默认值说明
macula.web.exception-advicetrue启用统一异常处理
macula.web.response-advicetrue启用统一响应包装
macula.jackson.long-to-stringtrue将 Long、BigInteger、BigDecimal 序列化为字符串
macula.jackson.null-to-emptyfalse按字段类型将 null 输出为空值
macula.jackson.local-date-time-format未设置自定义 LocalDateTime 格式
macula.jackson.local-date-format未设置自定义 LocalDate 格式
macula.jackson.local-time-format未设置自定义 LocalTime 格式

通用 Jackson 开关使用 spring.jackson.*。

核心功能

JSON 转换器显式设置默认字符集为 UTF-8,自动生成的 JSON 响应头包含 charset=UTF-8;SSE 专用 PrettyPrinter 在构造时初始化并复用。

Jackson 3 请求反序列化保留基本类型字段接收 null 的历史行为,例如 int 转为 0、boolean 转为 false。开启 JSON 缩进时,SSE 响应的后续行仍使用 data: 前缀。

消息转换器通过 Spring 7 的 configureMessageConverters(HttpMessageConverters.ServerBuilder builder) 配置:字符串使用 UTF-8,JSON 使用 MappingApiJacksonHttpMessageConverter。通过 withStringConverter / withJsonConverter 替换对应默认转换器,其他格式保留框架默认注册行为。

升级自定义扩展时,将 AbstractReadWriteJackson2HttpMessageConverter、MappingApiJackson2HttpMessageConverter 分别替换为去掉名称中 2 的新类。Jackson API 使用 tools.jackson.*;共享注解仍使用 com.fasterxml.jackson.annotation.*。

全局异常处理

如果在Controller每个方法都catch异常,非常不方便,所以默认通过ControllerExceptionAdvice类处理,包括:

  • BindException(使用form data方式调用接口,校验异常抛出 BindException)
  • ConstraintViolationException(单个参数校验异常抛出ConstraintViolationException)
  • MethodArgumentNotValidException (使用 json 请求体调用接口,校验异常抛出 MethodArgumentNotValidException)
  • BizException(业务异常,一般业务可以继承这个异常类)
  • NullPointerException
  • Exception(兜底)

通常返回 HTTP 500;BizCheckException 返回 HTTP 510。失败结果的结构如下:

{
  "success": false,
  "code": "10001",
  "msg": "异常说明",
  "cause": "具体的异常信息",
  "data": null
}

BizException 异常的 message 通过 cause 字段返回,支持国际化 code。其定义如下:

public class BizException extends MaculaException {

   private String code;
   private String msg;

   public BizException(ResultCode resultCode, String exceptionMessage) {
      this(resultCode.getCode(), resultCode.getMsg(), exceptionMessage);
   }

   public BizException(String code, String msg, String exceptionMessage) {
      // message用于用户设置抛出错误详情,例如:当前价格-5,小于0
      super(exceptionMessage);
      this.code = code;
      this.msg = msg;
   }

   public BizException(String exceptionMessage) {
      this(ApiResultCode.BIZ_ERROR, exceptionMessage);
   }

   public String getCode() {
      return this.code;
   }

   public String getMsg() {
      return this.msg;
   }
}

大家在开发应用时,在service层可以讲底层异常使用BizException封装为业务异常抛出。在Controller层一般情况下无需catch任何异常,交由上述全局异常处理器统一处理。

全局返回结构处理

默认情况下所有Controller都按照Result结构体返回,Controller返回值无需自己转换为Result类,保持原始返回类即可。

如果不需要包装可以添加@NotControllerResponseAdvice注解。

Result类由macula-boot-commons模块定义。具体如下:


@Data
public class Result<T> implements Serializable {
   private boolean success;
   private String code;
   private String msg;
   private String cause;
   private T data;
}

并且为了code属性的规范一致,提供了ResultCode接口

public interface ResultCode {
   /**
    * 返回码
    */
   String getCode();

   /**
    * 返回消息
    */
   String getMsg();
}

macula默认提供了ApiResultCode实现,定义了用户类异常、业务类异常以及第三方调用异常。

建一个各项目自行定义自己的ResultCode,code的编码规则建议如下:

  • 长度为5位
  • 第一位字母,表示一类异常,macula保留A用户类异常、B业务类异常、C第三方调用异常等三个字母。
  • 后四位用数字表示,同类型的异常尽量保持前两位一致

Controller示例


@RestController
@AllArgsConstructor
@RequestMapping("/api/v1/consumer")
public class ConsumerController {

   @Autowired
   private Provider1Service provider1Service;

   @Autowired
   private GapiService gapiService;

   @Autowired
   private IpaasService ipaasService;

   @GetMapping("/echo")
   @NotControllerResponseAdvice
   // 该接口直接返回String
   public String echo() {
      return provider1Service.echo("consumer");
   }

   @PostMapping("/user")
   // 该接口返回的JSON是{sucess:true, code:xxx, msg: xxx, data: {UserResult}}
   public UserResult getUser() {
      return provider1Service.getUser();
   }
}

授权列表的资源凭证扩展

现有 ControllerResponseAdvice 支持 Commons 的 PasskeyResource 和 PasskeyIssuer 扩展契约,无需 Web Starter 依赖 Security Starter。Collection、PageVO、成功 Result 包装中的接口实现元素会调用签发器;ResponseEntity 正文同样处理。不同 DTO 可提供相同的全局唯一编号;混合列表的普通元素保持不变,单个对象不自动签发。辅助 resourceId 不输出 JSON,passkey 正常输出。

返回接口实现元素即表示业务已按当前用户过滤资源访问范围。 无需签发注解,不得将此类 DTO 用于未经授权的列表;每次响应覆盖旧凭证,不将缓存凭证当作授权依据。macula.security.passkey.enabled=false(默认)时跳过列表遍历和签发,列表正常返回,已有自定义签发器也不会被调用。只有启用时,有候选元素但无有效签发器才拒绝;默认 Security 服务启用时要求有效签名配置。

签发在包装判定前执行:Result、ResponseEntity、Feign 或 @NotControllerResponseAdvice 的包装跳过不能绕过签发,但保持原响应外壳。原两参 Advice 构造入口仍保留,无签发提供者时仍可处理普通响应。关闭 macula.web.response-advice 或提供自定义 Advice 后,自动签发需自行接入,资源前置验证不会因此放行。

现有 ControllerExceptionAdvice 将实现公共 ResourceAccessDenied 标记的异常映射为 HTTP 403 / A0312,其他异常沿用原处理。完整配置和单注解验证见 Security Starter。

国际化

客户端透过请求头的Accept-Language设定对应的国际化资源,其他参考Spring标准。

时间格式

日期传入

http请求传入日期格式的字符串

表单请求

当GET请求或者POST请求的x-www-form-urlencoded时,前端的时间格式都是通过Converter转为日期格式,系统默认已经设置为ISO日期格式。

@Override public void addFormatters(FormatterRegistry registry){
        // 默认String没有办法转为java.util.Date类型
        registry.addConverter(new Converter<String, Date>(){
@Override public Date convert(String source){
        return DateUtil.parse(source.trim()).toJdkDate();
        }
        });

        DateTimeFormatterRegistrar registrar=new DateTimeFormatterRegistrar();
        registrar.setUseIsoFormat(true);
        registrar.registerFormatters(registry);
        }

enum ISO {
   /**
    * The most common ISO Date Format {@code yyyy-MM-dd} &mdash; for example,
    * "2000-10-31".
    */
   DATE,

   /**
    * The most common ISO Time Format {@code HH:mm:ss.SSSXXX} &mdash; for example,
    * "01:30:00.000-05:00".
    */
   TIME,

   /**
    * The most common ISO Date Time Format {@code yyyy-MM-dd'T'HH:mm:ss.SSSXXX}
    * &mdash; for example, "2000-10-31T01:30:00.000-05:00".
    */
   DATE_TIME
}
JSON请求

如果是POST请求的application/json时,则由jackson的配置来完成

spring:
  jackson:
     date-format: yyyy-MM-dd HH:mm:ss        # 该配置只会影响java.util.Date类型
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") private LocalDateTime createTime;

日期返回

普通响应

响应如果是text/html时,由生成html的模板引擎配置

JSON响应

响应如果是json格式也是由jackson的配置

数据库日期

数据库和Java的实体建议使用无时区的字段,mysql的date对应LocalDate,time对应LocalTime,datetime对应LocalDateTime,timestamp对应ZonedDateTime(不建议用),jdbc url要配置serverTimeZone和数据库时区保持一致

// 转为LocalDateTime需要先把ZondeDateTime转为需要的时区,直接toLocalDateTime是没有换算时区的
LocalDateTime ldt=zdt.withZoneSameInstant(ZoneId.systemDefault()).toLocalDateTime();

Validator校验

默认引入了spring-boot-starter-validation,可以在这里继续扩展自定义的validator,比如身份证、电话、邮件等等。

public class UserVO {
   @NotNull(message = "age 不能为空")
   private Integer age;
}

然后在controller方法中添加@Validated:

// 注意这里的BindingResult参数可以不添加,如果不添加则validator有问题会抛出异常,由全局异常拦截器统一处理
public String add1(@Validated UserVO userVO,BindingResult result){
        List<FieldError> fieldErrors=result.getFieldErrors();
        if(!fieldErrors.isEmpty()){
        return fieldErrors.get(0).getDefaultMessage();
        }
        return"OK";
        }

内置的校验注解有很多,罗列如下:

注解校验功能
@AssertFalse必须是false
@AssertTrue必须是true
@DecimalMax小于等于给定的值
@DecimalMin大于等于给定的值
@Digits可设定最大整数位数和最大小数位数
@Email校验是否符合Email格式
@Future必须是将来的时间
@FutureOrPresent当前或将来时间
@Max最大值
@Min最小值
@Negative负数(不包括0)
@NegativeOrZero负数或0
@NotBlank不为null并且包含至少一个非空白字符
@NotEmpty不为null并且不为空
@NotNull不为null
@Null为null
@Past必须是过去的时间
@PastOrPresent必须是过去的时间,包含现在
@Pattern必须满足正则表达式
@PositiveOrZero正数或0
@Size校验容器的元素个数

分组校验

如果同一个参数,需要在不同场景下应用不同的校验规则,就需要用到分组校验了。比如:新注册用户还没起名字,我们允许name 字段为空,但是不允许将名字更新为空字符。

分组校验有三个步骤:

  1. 定义一个分组类(或接口)
  2. 在校验注解上添加groups属性指定分组
  3. Controller方法的@Validated注解添加分组类
public interface Update extends Default{
}
public class UserVO {
    @NotBlank(message = "name 不能为空",groups = Update.class)
    private String name;
    // 省略其他代码...
}
@PostMapping("update")
public ResultInfo update(@Validated({Update.class}) UserVO userVO) {
    return new ResultInfo().success(userVO);
}

细心的同学可能已经注意到,自定义的Update分组接口继承了Default接口。校验注解(如: @NotBlank)和@validated 默认都属于Default.class分组,这一点在jakarta.validation.groups.Default注释中有说明

/**
 * Default Jakarta Bean Validation group.
 * <p>
 * Unless a list of groups is explicitly defined:
 * <ul>
 *     <li>constraints belong to the {@code Default} group</li>
 *     <li>validation applies to the {@code Default} group</li>
 * </ul>
 * Most structural constraints should belong to the default group.
 *
 * @author Emmanuel Bernard
 */
public interface Default {
}

在编写Update分组接口时,如果继承了Default,下面两个写法就是等效的:

@Validated({Update.class})
@Validated({Update.class,Default.class})

请求一下/update接口可以看到,不仅校验了name字段,也校验了其他默认属于Default.class分组的字段

{
    "status": 400,
    "message": "客户端请求参数错误",
    "response": [
        "name 不能为空",
        "age 不能为空",
        "email 不能为空"
    ]
}

如果Update不继承Default,@Validated({Update.class})就只会校验属于Update.class分组的参数字段,修改后再次请求该接口得到如下结果,可以看到, 其他字段没有参与校验:

{
    "status": 400,
    "message": "客户端请求参数错误",
    "response": [
        "name 不能为空"
    ]
}

递归校验

如果 UserVO 类中增加一个 OrderVO 类的属性,而 OrderVO 中的属性也需要校验,就用到递归校验了,只要在相应属性上增加@Valid 注解即可实现(对于集合同样适用)

OrderVO类如下

public class OrderVO {
   @NotNull
   private Long id;
   @NotBlank(message = "itemName 不能为空")
   private String itemName;
   // 省略其他代码...
}

在 UserVO 类中增加一个 OrderVO 类型的属性

public class UserVO {
   @NotBlank(message = "name 不能为空", groups = Update.class)
   private String name;
   //需要递归校验的OrderVO
   @Valid
   private OrderVO orderVO;
   // 省略其他代码...
}   

自定义校验

Spring 的 validation 为我们提供了这么多特性,几乎可以满足日常开发中绝大多数参数校验场景了。但是,一个好的框架一定是方便扩展的。有了扩展能力,就能应对更多复杂的业务场景,毕竟在开发过程中, 唯一不变的就是变化本身。

Spring Validation允许用户自定义校验,实现很简单,分两步:

  1. 自定义校验注解
  2. 编写校验者类

代码也很简单,结合注释你一看就能懂

@Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER})
@Retention(RUNTIME)
@Documented
@Constraint(validatedBy = {HaveNoBlankValidator.class})// 标明由哪个类执行校验逻辑
public @interface HaveNoBlank {
    
    // 校验出错时默认返回的消息
    String message() default "字符串中不能含有空格";

    Class<?>[] groups() default { };

    Class<? extends Payload>[] payload() default { };

    /**
     * 同一个元素上指定多个该注解时使用
     */
    @Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE })
    @Retention(RUNTIME)
    @Documented
    public @interface List {
        NotBlank[] value();
    }
}
public class HaveNoBlankValidator implements ConstraintValidator<HaveNoBlank, String> {
    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        // null 不做检验
        if (value == null) {
            return true;
        }
        if (value.contains(" ")) {
            // 校验失败
            return false;
        }
        // 校验成功
        return true;
    }
}

自定义校验注解使用起来和内置注解无异,在需要的字段上添加相应注解即可,同学们可以自行验证

字段脱敏

通过使用@Sensitive注解可以令某个字段属性在序列化为JSON时按照一定的规则脱敏,并且预置了如下规则:

  • MOBILE:手机号,如:138****2359
  • CHINESE_NAME:中文名 ,如:黄**
  • ID_CARD:身份证号,如:如511623********0537
  • FIXED_PHONE:座机号,如:****1234
  • ADDRESS:地址,如:广东省广州市天河区****
  • EMAIL:电子邮件,如:g**@163.com
  • BANK_CARD:银行卡,如:如622848*********5579
  • CUSTOM_HIDE:自定义,有多少个字符替换成多少个*
  • CUSTOM_RETAIN_HIDE:保留方式隐藏
  • CUSTOM_OVERLAY:自定义,只替换成指定个* 例如:
@Data
public class DemoDto {

   @NotEmpty
   private String password;

   @Length(min = 5, max = 25, message = "{key.length}")
   private String key;

   @Pattern(regexp = "[012]", message = "无效的状态标志")
   private String state;

   @Sensitive(value = Sensitive.Type.MOBILE)
   private String mobile;

}

@Sensitive注解定义:

/**
 * {@code Sensitvie} 脱敏的JSON注解
 *
 * @author rain
 * @since 2022/7/28 23:48
 */
@Target({ElementType.FIELD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@JacksonAnnotationsInside
@JsonSerialize(using = SensitiveSerializer.class)
public @interface Sensitive {

   /**
    * 脱敏的类型,默认手机号
    *
    * @return 返回脱敏类型
    */
   Type value();

   /**
    * CUSTOM_HIDE/CUSTOM_OVERLAY 时生效
    *
    * @return 开始位置(包含)
    */
   int startInclude() default 0;

   /**
    * CUSTOM_HIDE/CUSTOM_OVERLAY 时生效
    *
    * @return 结束位置(不包含)
    */
   int endExclude() default 0;

   /**
    * CUSTOM_OVERLAY 时生效
    *
    * @return *重复的次数
    */
   int overlayRepeat() default 4;

		...
}

Jackson配置

默认对Jackson做了如下配置

@Bean
@ConditionalOnMissingBean
public JsonMapperBuilderCustomizer customizer() {
    return builder -> {
        builder.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
        builder.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
        SimpleModule module = new SimpleModule("customJacksonModule");
        module.addSerializer(Long.class, ToStringSerializer.instance);
        builder.addModule(module);
    };
}

Macula Boot 6.1 使用 Jackson 3:JsonMapperBuilderCustomizer 位于 org.springframework.boot.jackson.autoconfigure,Jackson 数据绑定类型位于 tools.jackson.databind。 Boot 3 的 Jackson2ObjectMapperBuilderCustomizer 与 com.fasterxml.jackson.databind 扩展不能直接用于该配置链。

租户ID

在HTTP请求头或者请求参数中添加tenantId参数可以设置当前请求的租户ID上下文,用户后续的数据库操作按照租户ID隔离数据。一般用在前端界面切换不同租户来管理。

依赖引入


<dependencies>
   <dependency>
      <groupId>dev.macula.boot</groupId>
      <artifactId>macula-boot-commons</artifactId>
   </dependency>
   <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
   </dependency>
   <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-validation</artifactId>
   </dependency>
</dependencies>

版权说明

  • Spring:https://github.com/spring-projects/spring-boot/blob/main/LICENSE.txt