21 changed files with 281 additions and 57 deletions
@ -0,0 +1,49 @@ |
|||||||
|
package com.mh.user.config; |
||||||
|
|
||||||
|
import com.fasterxml.jackson.core.JsonGenerator; |
||||||
|
import com.fasterxml.jackson.core.JsonParser; |
||||||
|
import com.fasterxml.jackson.databind.DeserializationContext; |
||||||
|
import com.fasterxml.jackson.databind.DeserializationFeature; |
||||||
|
import com.fasterxml.jackson.databind.JsonDeserializer; |
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper; |
||||||
|
import com.fasterxml.jackson.databind.module.SimpleModule; |
||||||
|
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; |
||||||
|
import com.mh.user.model.MyBigDecimalDeserializer; |
||||||
|
import org.springframework.context.annotation.Bean; |
||||||
|
import org.springframework.context.annotation.Configuration; |
||||||
|
|
||||||
|
import java.io.IOException; |
||||||
|
import java.math.BigDecimal; |
||||||
|
|
||||||
|
/** |
||||||
|
* @author LJF |
||||||
|
* @version 1.0 |
||||||
|
* @project CHWS |
||||||
|
* @description 避免使用科学计数算法 |
||||||
|
* @date 2026-01-07 14:37:11 |
||||||
|
*/ |
||||||
|
@Configuration |
||||||
|
public class MyJacksonConfig { |
||||||
|
|
||||||
|
@Bean("customObjectMapper") |
||||||
|
public ObjectMapper customObjectMapper() { |
||||||
|
ObjectMapper mapper = new ObjectMapper(); |
||||||
|
|
||||||
|
// 注册 JavaTimeModule
|
||||||
|
mapper.registerModule(new JavaTimeModule()); |
||||||
|
|
||||||
|
// 启用 BigDecimal 作为普通格式输出(避免科学计数法)
|
||||||
|
mapper.enable(JsonGenerator.Feature.WRITE_BIGDECIMAL_AS_PLAIN); |
||||||
|
|
||||||
|
// 注册自定义反序列化器
|
||||||
|
SimpleModule module = new SimpleModule(); |
||||||
|
module.addDeserializer(BigDecimal.class, new MyBigDecimalDeserializer()); |
||||||
|
mapper.registerModule(module); |
||||||
|
|
||||||
|
// 启用 BigDecimal 处理浮点数
|
||||||
|
mapper.enable(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS); |
||||||
|
|
||||||
|
return mapper; |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
@ -0,0 +1,34 @@ |
|||||||
|
package com.mh.user.model; |
||||||
|
|
||||||
|
import com.fasterxml.jackson.core.JsonParser; |
||||||
|
import com.fasterxml.jackson.databind.DeserializationContext; |
||||||
|
import com.fasterxml.jackson.databind.JsonDeserializer; |
||||||
|
|
||||||
|
import java.io.IOException; |
||||||
|
import java.math.BigDecimal; |
||||||
|
|
||||||
|
/** |
||||||
|
* @author LJF |
||||||
|
* @version 1.0 |
||||||
|
* @project CHWS |
||||||
|
* @description 格式化 |
||||||
|
* @date 2026-01-07 14:20:36 |
||||||
|
*/ |
||||||
|
public class MyBigDecimalDeserializer extends JsonDeserializer<BigDecimal> { |
||||||
|
|
||||||
|
@Override |
||||||
|
public BigDecimal deserialize(JsonParser p, DeserializationContext ctxt) throws IOException { |
||||||
|
String value = p.getValueAsString(); |
||||||
|
if (value == null || value.trim().isEmpty()) { |
||||||
|
return BigDecimal.ZERO; |
||||||
|
} |
||||||
|
|
||||||
|
try { |
||||||
|
// 直接使用字符串构造BigDecimal,能正确处理科学计数法
|
||||||
|
return new BigDecimal(value); |
||||||
|
} catch (NumberFormatException e) { |
||||||
|
// 如果转换失败,返回0
|
||||||
|
return BigDecimal.ZERO; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,33 @@ |
|||||||
|
package com.mh.user.model; |
||||||
|
|
||||||
|
import com.fasterxml.jackson.databind.annotation.JsonDeserialize; |
||||||
|
import lombok.Data; |
||||||
|
|
||||||
|
import java.math.BigDecimal; |
||||||
|
|
||||||
|
/** |
||||||
|
* @author LJF |
||||||
|
* @version 1.0 |
||||||
|
* @project EEMCS |
||||||
|
* @description 三石峰数据体 |
||||||
|
* @date 2025-01-22 14:47:25 |
||||||
|
*/ |
||||||
|
@Data |
||||||
|
public class SanShiFengStrDatas { |
||||||
|
|
||||||
|
/** |
||||||
|
* 对应研华的标签值 |
||||||
|
*/ |
||||||
|
private String name; |
||||||
|
|
||||||
|
/** |
||||||
|
* 上报值 |
||||||
|
*/ |
||||||
|
// 使用自定义反序列化器处理科学计数法
|
||||||
|
private String value; |
||||||
|
// /**
|
||||||
|
// * 质量值
|
||||||
|
// */
|
||||||
|
// private T quality;
|
||||||
|
|
||||||
|
} |
||||||
Loading…
Reference in new issue