Jersey / jaxb problem with long precision

We use Jersey / Json (v1.12) for the JAX-RS web service, and we output longs as part of some simple sorting of JAXB objects. Today we noticed something, and I looked around to find out if this was a common problem, but debugging failed.

One of the objects we use is long as a unique identifier (according to the java specs, 2 ^ 63-1), which is ~ 9223372036854775807 (19 digits or so).

If we try to print a long one (17 or less digits), we will get it as it should (correctly).

If we try to print a long one (18 digits to 2 ^ 63-1), we get the wrong conclusion - it seems to round the last 2/3 significant digits, for example:

@GET @Path("/longTest") @Produces("application/json") public JSONObject testLong() { JSONObject myObject = new JSONObject(); try { myObject.put("1L", 1); myObject.put("13365766603759910L", 13365766603759910L); myObject.put("133614582656610538L", 133614582656610538L); myObject.put("9133614582656610538L", 9133614582656610538L); } catch (JSONException e) { e.printStackTrace(); } return myObject; } 

returns:

 { "1L":1, "13365766603759910L":13365766603759910, "133614582656610538L":133614582656610540, "9133614582656610538L":9133614582656610000 } 

Pay attention to the wrapping of the last two numbers (538-540 (18 digits) and 538,000 (19 digits)).

If I marshall long for json directly, it works fine:

 @GET @Path("/longTest3") @Produces("application/json") public long testLong3() { return 133614582656610538L; } 

returns:

 133614582656610538 

I added POJO JSON support when initializing my web server.

Should I use another JSONConfiguration or JAXBContextResolver for this?

Thanks in advance for your help!

+4
source share

Source: https://habr.com/ru/post/1412026/


All Articles