自定义注解格式化处理BigDecimal

目录

一、场景描述

二、实现步骤

1、自定义类

2、使用注解

3、测试


一、场景描述

        在开发过程中,如果有实体类的属性中存在BigDecimal的属性,并且需要对其进行校验,例如:限制BigDecimal的小数点位数是两位。但目前没有注解可以完成这个操作,因此我们可以使用下面的工具类和注解进行处理。

二、实现步骤

1、自定义类

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;


public class BigDecimalSerializer extends JsonSerializer<BigDecimal> {

    @Override
    public void serialize(BigDecimal value, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
        if (value != null) {
            // 保留2位小数,四舍五入
            BigDecimal number = value.setScale(2, RoundingMode.HALF_UP);
            jsonGenerator.writeNumber(number);
        } else {
            jsonGenerator.writeNumber(value);
        }
    }
}

2、使用注解


import com.example.yht.utils.BigDecimalSerializer;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import lombok.Data;
import lombok.ToString;

import java.math.BigDecimal;
@Data
@ToString
public class Student {
    private String name;

    private Integer age;

    private String address;

    @JsonSerialize(using = BigDecimalSerializer.class)
    private BigDecimal score;
}

3、测试

@RestController
public class TestController {
    @PostMapping("/test")
    public Student test(@RequestBody Student student){
        return student;
    }
}

测试结果:

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果您想自定义一个注解,使其可以将 `BigDecimal` 对象保留指定的小数位数,您可以按照以下步骤进行操作: 首先,定义一个 `@TwoDecimalPlaces` 注解: ```java import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD) public @interface TwoDecimalPlaces { int value() default 2; } ``` 在注解中,我们定义了一个 `value` 属性,用于指定要保留的小数位数,默认值为 2。 然后,在您的代码中,您可以使用 `@Inject` 注入 `ConfigProvider` 对象,并在需要使用 `BigDecimal` 时将其保留到指定的小数位数: ```java import javax.inject.Inject; import org.eclipse.microprofile.config.inject.ConfigProperty; public class MyService { @Inject ConfigProvider configProvider; public void doSomething() { BigDecimal value = new BigDecimal("123.456"); TwoDecimalPlaces annotation = getClass().getDeclaredField("value").getAnnotation(TwoDecimalPlaces.class); int scale = annotation != null ? annotation.value() : configProvider.getConfig().getValue("myapp.decimal.scale", Integer.class); value = value.setScale(scale, BigDecimal.ROUND_HALF_UP); // ... } } ``` 在此示例中,我们首先使用 `getClass().getDeclaredField("value").getAnnotation(TwoDecimalPlaces.class)` 方法获取 `@TwoDecimalPlaces` 注解,如果未找到该注解,则使用 `ConfigProvider` 获取 `myapp.decimal.scale` 配置值作为保留的小数位数。 这样,您就可以在代码中使用 `@TwoDecimalPlaces` 注解来指定要保留的小数位数了: ```java public class ExampleClass { @TwoDecimalPlaces(value = 3) private BigDecimal value; // ... } ``` 在此示例中,我们将 `value` 字段上的 `@TwoDecimalPlaces` 注解设置为保留 3 位小数,因此在使用 `doSomething` 方法时,`value` 将被设置为保留 3 位小数的 `BigDecimal` 对象。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值