- 根据业务,需要通过后端给前端返回部分double类型的数值,一般需要保留两位小数,使用jackson转换对象
java
复制代码
package com.ruoyi.common.core.config;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import org.apache.commons.lang3.math.NumberUtils;
import java.io.IOException;
import java.math.RoundingMode;
public class CustomDoubleSerialize extends JsonSerializer<Double> {
@Override
public void serialize(Double doubleVal, JsonGenerator gen, SerializerProvider serializers) throws IOException{
if(doubleVal != null) {
gen.writeNumber(NumberUtils.toScaledBigDecimal(doubleVal, 2, RoundingMode.HALF_UP).doubleValue());
}
}
}
java
复制代码
package com.demo.domain.vo;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.ruoyi.common.core.config.CustomDoubleSerialize;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import java.io.Serializable;
@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString
@ApiModel("类比Vo")
public class OcsAnalogyVo implements Serializable{
@ApiModelProperty("节点/班组id")
private Long showId;
@ApiModelProperty("节点/班组名称")
private String showName;
@ApiModelProperty("展示值")
@JsonSerialize(using = CustomDoubleSerialize.class)
private Double showVal;
@ApiModelProperty("展示时间")
private String showTime;
}