springboot 返回前端数据,利用jackson全局配置将long转换为string,防止精度丢失的代码
时间: 2023-06-09 20:06:20 浏览: 171
可以在SpringBoot项目中的配置类中添加以下代码,实现将Long类型转换为String类型,并避免精度丢失的问题。
```java
@Configuration
public class JacksonConfig {
@Bean
public ObjectMapper objectMapper() {
ObjectMapper objectMapper = new ObjectMapper();
SimpleModule module = new SimpleModule();
module.addSerializer(Long.class, ToStringSerializer.instance);
module.addSerializer(Long.TYPE, ToStringSerializer.instance);
objectMapper.registerModule(module);
return objectMapper;
}
}
```
以上代码通过注册自定义的Jackson模块,将Long类型序列化为String类型。这样在SpringBoot项目中返回JSON数据时,Long类型的数据将会默认转换为String类型,避免了精度丢失的问题。
相关问题
java Jackson全局转化long类型为String 排除IPage类属性不转换
可以通过自定义Jackson的ObjectMapper来实现。具体实现如下:
1. 定义一个自定义的ObjectMapper,继承自Jackson的ObjectMapper:
```java
public class LongToStringObjectMapper extends ObjectMapper {
public LongToStringObjectMapper() {
SimpleModule module = new SimpleModule();
// 设置序列化规则,将Long类型转为String类型
module.addSerializer(Long.class, ToStringSerializer.instance);
module.addSerializer(Long.TYPE, ToStringSerializer.instance);
// 反序列化时不做任何操作
module.addDeserializer(Long.class, new JsonDeserializer<>() {
@Override
public Long deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
return p.getValueAsLong();
}
});
this.registerModule(module);
}
}
```
2. 在Spring Boot中配置这个自定义的ObjectMapper:
```java
@Configuration
public class JacksonConfig {
@Bean
public ObjectMapper objectMapper() {
LongToStringObjectMapper objectMapper = new LongToStringObjectMapper();
// 排除IPage类属性不转换
objectMapper.addMixInAnnotations(IPage.class, IgnoreLongTypeInPage.class);
return objectMapper;
}
@JsonFilter("ignoreLongTypeInPage")
interface IgnoreLongTypeInPage {}
}
```
在上面的代码中,我们通过@JsonFilter注解创建了一个名为ignoreLongTypeInPage的过滤器,在IPage类上使用该注解来标记需要排除的属性,即可实现全局转化long类型为String,但排除IPage类属性不转换。
springboot LocalDateTime 如何返回前端为string
你可以使用 `@JsonFormat` 注解将 `LocalDateTime` 类型的字段转换为字符串格式,并返回给前端。在你的实体类中,可以这样使用:
```java
import com.fasterxml.jackson.annotation.JsonFormat;
public class YourEntity {
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime yourDateTime;
// getter and setter methods
}
```
上述代码中,`pattern` 属性定义了日期时间的格式。你可以根据需要进行调整,比如使用 `"yyyy-MM-dd"` 作为日期格式。
接下来,在你的控制器中,将实体对象转换为 JSON 格式返回给前端:
```java
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class YourController {
@GetMapping("/your-data")
public YourEntity getData() {
YourEntity data = new YourEntity();
// 设置 LocalDateTime 字段的值
return data;
}
}
```
当你访问 `/your-data` 路由时,会返回一个 JSON 格式的响应。其中,`yourDateTime` 字段会以字符串形式表示。
希望能够帮助到你!如果还有其他问题,请随时提问。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)