简体   繁体   中英

How to retrieve a Long value from WebTau response body Map<String,?> representation?

I am retrieving the JSON request body from WebTau as a Map, eg:

Map<String,?>  approvalMap = WebTauDsl.http.post(  
    callUrl,  restCallHeader, restCallBody, 
    ((header, body) -> 
    {
        return body;
    }
);
if( accessApprovalMap.get("id") instanceof Integer  )
{
    logger.info("id is Integer");
}
else if( accessApprovalMap.get("id") instanceof Long  )
{
    logger.info("id is Long");
}

From the logging code after the return ...

MonitorTest  INFO  : id is Integer

The question is what happens when the result is larger than MAX_INT is returned? BIGINT isn't very common at this point, but how would 'we' know? The string just looks like a number.

  • Is there a way to override the type of a JSON field?

related

Behind the scenes WebTau uses com.fasterxml.jackson to parse JSON. It automatically handles types like Long , Double , etc

Here is a WebTau test to show numbers conversion.

Given JSON response

{
  "intValue": 30000,
  "doubleValue": 100.43,
  "longValue": 9223372036854775807
}
@Test
public void conversionOfNumbers() {
    Map<String, ?> bodyAsMap = http.get("/large-numbers", (header, body) -> {
        body.get("longValue").should(equal(9223372036854775807L));
        body.get("doubleValue").should(equal(100.43));
        body.get("intValue").should(equal(30000));

        return body;
    });

    actual(bodyAsMap.get("longValue").getClass()).should(equal(Long.class));
    actual(bodyAsMap.get("doubleValue").getClass()).should(equal(Double.class));
    actual(bodyAsMap.get("intValue").getClass()).should(equal(Integer.class));
}

示例控制台输出

I am thinking that your response may not have a long enough number in your test.

If you provide more info on what you plan to do with the actual numbers, I may suggest an alternative way of achieving it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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