롱 타 입 16 비트 이상 의 정밀도 문 제 를 해결 하 는 두 가지 방법
4495 단어 구 덩이 를 밟다
클래스 LongJSonSerializer 를 추가 합 니 다. 코드 는 다음 과 같 습 니 다.
1 2 3 4 5 6 7 8 9 10 11 12 13
/** * Long , js * */ 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); } } } 따라서 가방 에 LongJSon Deserializer 를 추가 합 니 다. 코드 는 다음 과 같 습 니 다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/** * Long * */ public class LongJsonDeserializer extends JsonDeserializer { private static final Logger logger = LoggerFactory.getLogger(LongJsonDeserializer. class ); @Override public Long deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException { String value = jsonParser.getText(); try { return value == null ? null : Long.parseLong(value); } catch (NumberFormatException e) { logger.error( " " , e); return null ; } } } 다음은 이 두 가지 종 류 를 사용한다.
처리 해 야 할 id 필드 에 주 해 를 추가 합 니 다.
1 2 3 4 5 6
/** * id */ @JsonSerialize( using = LongJsonSerializer. class ) @JsonDeserialize( using = LongJsonDeserializer. class ) private Long id; 두 번 째: 전역 설정, 이 설정 은 모든 지정 한 형식의 필드 에 영향 을 줍 니 다. 예 를 들 어 Long 이나 Date 등 입 니 다.
@Configuration
public class LongToJsonConfig extends WebMvcConfigurationSupport {
/**
* springboot
* long、bigint json
*/
public void configureMessageConverters(List> converters) {
MappingJackson2HttpMessageConverter jackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
ObjectMapper objectMapper = new ObjectMapper();
SimpleModule simpleModule = new SimpleModule();
/** * Long string( java Long ) */
simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
/** * json , long string * js long */
simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance);
objectMapper.registerModule(simpleModule);
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
jackson2HttpMessageConverter.setObjectMapper(objectMapper);
converters.add(jackson2HttpMessageConverter);
} 또 하 나 는 설정 식:
설정 파일 에 다음 매개 변 수 를 설정 합 니 다.
#
spring.jackson.generator.write-numbers-as-strings=true
spring.jackson.date-format="yyyy-MM-dd HH:mm:ss"
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
롱 타 입 16 비트 이상 의 정밀도 문 제 를 해결 하 는 두 가지 방법첫 번 째: 일부 필드, id 또는 날짜 등 을 정확하게 지정 해 야 합 니 다. 클래스 LongJSonSerializer 를 추가 합 니 다. 코드 는 다음 과 같 습 니 다. 1 2 3 4 5 6 7 8 9 10...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.