简体   繁体   English

Java E/科学记数法在 GET 请求中丢失

[英]Java E/Scientific Notation gets lost in GET Request

i have a spring boot application which returns a Response Entity that holds a double value (0.00010).我有一个 spring 启动应用程序,它返回一个包含双精度值(0.00010)的响应实体。 Since this is mapped to 1.0E-4, i would like to get 1.0E-4.由于这映射到 1.0E-4,我想获得 1.0E-4。 Instead, i get 0.00.相反,我得到 0.00。 Is it possible that the Response Entity of a GET Request is not capable to return 1.0E-4? 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 );
    }

From the HTTP Get Request i get:从 HTTP 获取请求我得到:

      "calculateXYZPercent": 0.00,

but i want it to be:但我希望它是:

 "calculateXYZPercent": 1.0E-4,

A breakpoint at line return ResponseEntity.ok( mappedContract ); return ResponseEntity.ok( mappedContract );行处的断点shows that there, the value still is 1.0E-4.显示在那里,该值仍然是 1.0E-4。

Thank you guys in advance!提前谢谢你们!

The Problem was, that the custom JsonSerializer mapped it to 0.00问题是,自定义 JsonSerializer 将其映射到 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 ) );
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM