我有一个spring-boot应用程序,它返回一个包含双值(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 ) );
}
}