-2

i have a spring boot application which returns a Response Entity that holds a double value (0.00010). Since this is mapped to 1.0E-4, i would like to get 1.0E-4. Instead, i get 0.00. Is it possible that the Response Entity of a GET Request is not capable to return 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 );
    }

From the HTTP Get Request i get:

      "calculateXYZPercent": 0.00,

but i want it to be:

 "calculateXYZPercent": 1.0E-4,

A breakpoint at line return ResponseEntity.ok( mappedContract ); shows that there, the value still is 1.0E-4.

Thank you guys in advance!

tdog
  • 27
  • 1
  • 5

1 Answers1

0

The Problem was, that the custom JsonSerializer mapped it to 0.00

For the wanted field do:

@JsonSerialize( using = TestSerializer.class)
Double test;

with

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 ) );
    }

}

tdog
  • 27
  • 1
  • 5