前端接收long类型精度丢失

很坑爹 ™


//1.需要导入对应的包
   
            io.cucumber
            datatable-dependencies
            RELEASE
        

//2.引入这个两个类
//LongJsonDeserializer类
/**
 * 将接收的前端字符串类型转换成Long类型
 */
public class LongJsonDeserializer extends JsonDeserializer {
    @Override
    public Long deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
        String value = jsonParser.getText();
        try {
            return value == null ? null : Long.parseLong(value);
        } catch (NumberFormatException e) {
            return null;
        }
    }
}

//LongJsonSerializer 

/**
 * 向前端返回时将Long转成字符串
 */
public class LongJsonSerializer extends JsonSerializer {
    @Override
    public void serialize(Long value, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException, JsonProcessingException {
        String text = (value == null ? null : String.valueOf(value));
        if (text != null) {
            jsonGenerator.writeString(text);
        }
    }
}


//3.使用注键   在返回或接收的对象中
    /**
     * 创建人
     */
    @JsonDeserialize(using = LongJsonDeserializer.class)
    @JsonSerialize(using = LongJsonSerializer.class)
    @ApiModelProperty(value = "创建人")
    @Column(name = "create_id")
    private Long createId;

 

你可能感兴趣的:(Javaweb)