正常前端可以处理了bigint,如果前端无法解决该问题可以采取如下配置
第一步
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
/**
* <p>Title: JacksonMapper</p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2019</p>
*
* @author ctl
* @version 1.0
* @date 2020-04-15 22:11
*/
public class JacksonMapper extends ObjectMapper {
public JacksonMapper() {
super();
this.configure(JsonGenerator.Feature.WRITE_BIGDECIMAL_AS_PLAIN, true);
this.configure(JsonGenerator.Feature.IGNORE_UNKNOWN, true);
this.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
this.setSerializationInclusion(JsonInclude.Include.ALWAYS);
SimpleModule simpleModule = new SimpleModule();
simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance);
simpleModule.addSerializer(long.class, ToStringSerializer.instance);
registerModule(simpleModule);
}
}
第二步在springmvc配置文件配置 <mvc:annotation-driven> <mvc:message-converters> <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> <property name="objectMapper"> <bean class="JacksonMapper"></bean> </property> </bean> </mvc:message-converters> </mvc:annotation-driven>
spring-boot第二步可以使用下面替换 import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.http.HttpMessageConverters; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.converter.HttpMessageConverter; import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import java.util.List; /** * <p>Title: WebConfig</p> * <p>Description: </p> * <p>Copyright: Copyright (c) 2019</p> * * @author ctl * @version 1.0 * @date 2020-04-15 21:53 */ @Configuration public class WebConfig implements WebMvcConfigurer { @Autowired private HttpMessageConverters httpMessageConverters; /** * MappingJackson2HttpMessageConverter 实现了HttpMessageConverter 接口, * httpMessageConverters.getConverters() 返回的对象里包含了MappingJackson2HttpMessageConverter * @return */ @Bean public MappingJackson2HttpMessageConverter getMappingJackson2HttpMessageConverter() { return new MappingJackson2HttpMessageConverter(new JacksonMapper()); } @Override public void extendMessageConverters(List<HttpMessageConverter<?>> converters) { converters.addAll(httpMessageConverters.getConverters()); } }
另一种方式 Java传值给前端进行JSON序列化时,将Long 类型转成string 类型序列化。(推荐) 使用 @JsonSerialize(using = ToStringSerializer.class) 注解
/** * 主键 */ @JsonSerialize(using = ToStringSerializer.class) private Long id;
前端收到的是
"id":"9327445563932672"