[英]Java E/Scientific Notation gets lost in GET Request
我有一个 spring 启动应用程序,它返回一个包含双精度值(0.00010)的响应实体。 由于这映射到 1.0E-4,我想获得 1.0E-4。 相反,我得到 0.00。 GET 请求的响应实体是否可能无法返回 1.0E-4?
@GetMapping( path = "/ui/contract/XYZ/{contractNumber}", produces = { MediaType.APPLICATION_JSON_VALUE } )
@EndpointAuthorization( action = ViewContract.class )
public ResponseEntity<Contract> getXYZContract( @ValidContractNumber @PathVariable String contractNumber ) throws IOException {
return getContractResponseEntity( contractNumber );
}
private ResponseEntity<Contract> getContractResponseEntity( String contractNumber ) {
log.debug( "getContract( {} )", contractNumber );
var contract = contractService.getContract( restContext.getPermissionContext(), contractNumber );
if( contractService.hasAllActions( contract, Set.of( ViewContract.class, ViewCustomer.class ) ) ) {
accessLogService.saveAccessLog( restContext.getPersonId(), contract );
}
Contract mappedContract = ContractMapper.map( contract );
log.debug( "Return XYZ-Contract: {}", mappedContract );
return ResponseEntity.ok( mappedContract );
}
从 HTTP 获取请求我得到:
"calculateXYZPercent": 0.00,
但我希望它是:
"calculateXYZPercent": 1.0E-4,
return ResponseEntity.ok( mappedContract );
行处的断点显示在那里,该值仍然是 1.0E-4。
提前谢谢你们!
问题是,自定义 JsonSerializer 将其映射到 0.00
对于想要的领域,请执行以下操作:
@JsonSerialize( using = TestSerializer.class)
Double test;
和
public class TestSerializer extends JsonSerializer<Double> {
@Override
public void serialize( Double value, JsonGenerator gen, SerializerProvider serializers ) throws IOException {
gen.writeNumber( BigDecimal.valueOf( value ).setScale( 5, RoundingMode.HALF_UP ) );
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.