新增注解
import com.dbl.util.DecimalFormatJsonDeserializer;
import com.fasterxml.jackson.annotation.JacksonAnnotationsInside;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
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, ElementType.METHOD})
@JacksonAnnotationsInside
@JsonDeserialize(using = DecimalFormatJsonDeserializer.class)
public @interface DecimalFormat {
int scale() default 2;
boolean isThrowException() default true;
double defaultVal() default 0.0;
RoundingMode roundMode() default RoundingMode.HALF_UP;
}
测试用实体类
import com.dbl.annotation.DecimalFormat;
import lombok.Data;
@Data
public class Bill {
@DecimalFormat(scale = 2)
private Double money;
}
序列化转换类
import com.dbl.annotation.DecimalFormat;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import java.io.IOException;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class DecimalFormatJsonDeserializer extends JsonDeserializer<Object> {
@Override
public Object deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
boolean isthrowException = true;
double defaultVal = 0.0;
Class<?> type = null;
boolean isException = false;
try {
String numStr = jsonParser.getText();
Class<?> clazz = jsonParser.getCurrentValue().getClass();
Field field = clazz.getDeclaredField(jsonParser.getCurrentName());
DecimalFormat annotation = field.getAnnotation(DecimalFormat.class);
int scale = annotation.scale();
isthrowException = annotation.isThrowException();
defaultVal = annotation.defaultVal();
RoundingMode roundingMode = annotation.roundMode();
roundingMode = roundingMode == null ? RoundingMode.HALF_UP : roundingMode;
type = field.getType();
if (StringUtils.isEmpty(numStr) || !isNumeric(numStr)) {
if (type == String.class) {
return String.valueOf(defaultVal);
} else {
return defaultVal;
}
}
BigDecimal decimalValue = new BigDecimal(numStr);
if (type == String.class) {
return decimalValue.setScale(scale, roundingMode).toString();
} else {
return decimalValue.setScale(scale, roundingMode).doubleValue();
}
} catch (Exception e) {
log.error("数字格式转换异常:", e);
if (isthrowException) {
isException = true;
throw e;
} else {
if (type == String.class) {
return String.valueOf(defaultVal);
} else {
return defaultVal;
}
}
}
}
public boolean isNumeric(String str) {
Pattern pattern = Pattern.compile("-?[0-9]+.?[0-9]*");
Matcher isNum = pattern.matcher(str);
if (!isNum.matches()) {
return false;
}
return true;
}
}
测试
import com.dbl.entity.Bill;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;
@RestController
public class Test {
@GetMapping
public Bill test(String money) throws IOException {
String json = "{\"money\":" + money + "}";
ObjectMapper mapper = new ObjectMapper();
Bill b = mapper.readValue(json, Bill.class);
System.out.println(b.getMoney());
return b;
}
}