接口返回JSON数据避免小数出现科学计数处理

问题描述

实际开发中,Java处理小数数据的时候,容易出现科学计数,原因因为结果长度超过限制或者出现0.0000000000x这种数据,结果就会导致接口返回8.88881E-16这种科学计数。

这里提供一种解决思路,也是参考别人的博客,实测能够解决问题!放心使用!

1、json使用fastjson
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.75</version>
</dependency>
2、自定义JsonSerializer,重写序列化方法

stripTrailingZeros() :去除尾部所有的0,并返回一个BigDecimal类型的数据,不能保证不是科学计数法。
toPlainString() : 不使用科学计数法

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;

public class CustomDoubleSerializer extends JsonSerializer {

    @Override
    public void serialize(Object o, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
        //转换成BigDecimal对象
        BigDecimal d = new BigDecimal(o.toString());
        //去除多余的尾部0,然后转换成不用科学计数的字符串,重写数据
        jsonGenerator.writeNumber(d.stripTrailingZeros().toPlainString());
    }
}
3、添加对象注解
@Data
public class StudentDTO {

    private Integer id;
    private String name;
    
    private Double data1;
    private BigDecimal dto1;

    @JsonSerialize(using = CustomDoubleSerializer.class)
    private Double data2;
    @JsonSerialize(using = CustomDoubleSerializer.class)
    private BigDecimal dto2;
}
4、测试调用(Springboot一个简单接口)
@GetMapping("/data")
public StudentDTO getJSONData(){

    StudentDTO dto = new StudentDTO();
    dto.setId(10001);
    dto.setName("测试数据");
    dto.setData1(new Double(0.000000000000000888881));
    dto.setDto1(new BigDecimal("0.00000000000000088881"));
    dto.setData2(new Double(0.000000000000000888881));
    dto.setDto2(new BigDecimal("0.00000000000000088881"));
    return dto;
}
5、测试结果(Postman调用)

在这里插入图片描述

参考博客: java科学记数法保留小数…

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要让 Java 接口返回 JSON 数据格式,你可以使用任何一个 JSON 库,比如 Jackson、Gson 或者 Fastjson。以下是使用 Jackson 库的示例代码: 首先,确保你的项目中已经引入了 Jackson 库的依赖。如果使用 Maven 管理项目依赖,可以在 pom.xml 文件中添加以下依赖: ```xml <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.12.3</version> </dependency> ``` 然后,在你的 Controller 中,可以按照如下方式编写代码: ```java import com.fasterxml.jackson.databind.ObjectMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class MyController { @Autowired private ObjectMapper objectMapper; @GetMapping("/my-api") public MyResponse myApi() { MyResponse response = new MyResponse(); response.setCode(200); response.setMessage("Success"); // 设置其它字段... return response; } public static class MyResponse { private int code; private String message; // 其它字段... // 省略 getter 和 setter 方法... public String toJson() { try { return objectMapper.writeValueAsString(this); } catch (Exception e) { throw new RuntimeException(e); } } } } ``` 以上代码中,我们使用 Jackson 库的 ObjectMapper 对象将 Java 对象转换为 JSON 字符串。在 MyResponse 类中,我们添加了 toJson() 方法,该方法将 MyResponse 对象转换为 JSON 字符串。 当客户端请求 /my-api 接口时,MyController 的 myApi() 方法返回 MyResponse 对象,并且 MyResponse 对象会被自动转换为 JSON 格式的数据返回给客户端。如果你想手动序列化对象为 JSON 字符串,可以调用 MyResponse.toJson() 方法。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

黑色的四叶草

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值