I have the following setup in my controller..
@RequestMapping(value="/security_param", method=RequestMethod.GET)
public @ResponseBody BigInteger getSecurityParam(){
return result.getBigInteger();
}
So, when I check my response by typing it in the browser, I got the following:
350094078853290372226929324000742644466642262681952226409886256271736198431807728267526406481556587992703721192341876290504971375383029475460844121232586227913173796191951197042895656298303777249177945550375699884594977303136892333029758596862673993846972456155271723432095094859391751714939658055297007851105568438549148984628844489662466256443824528144049402449678472642642...truncated
In my RestTemplate side, I had the JacksonMappingHttpMessageConverter setup like this:
CommonsClientHttpRequestFactory factory = new CommonsClientHttpRequestFactory(commonsClient);
RestTemplate httpClient2 = new RestTemplate(factory);
List<HttpMessageConverter<?>> converters = new ArrayList();
converters.add(new MappingJacksonHttpMessageConverter());
httpClient2.setMessageConverters(converters);
URL serverDomainUrl = new URL("http://127.0.0.1:8080");
String urlPath = new URL(serverDomainUrl, "/trusted_server/security_param").toString();
BigInteger xx = httpClient2.getForObject(urlPath, BigInteger.class);
Somehow, the deserialization doesn’t work well and I got the following error.
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 200
at org.codehaus.jackson.impl.Utf8StreamParser.parseNumberText(Utf8StreamParser.java:531)
at org.codehaus.jackson.impl.Utf8StreamParser._nextTokenNotInObject(Utf8StreamParser.java:435)
at org.codehaus.jackson.impl.Utf8StreamParser.nextToken(Utf8StreamParser.java:323)
at org.codehaus.jackson.map.ObjectMapper._initForReading(ObjectMapper.java:2439)
at org.codehaus.jackson.map.ObjectMapper._readMapAndClose(ObjectMapper.java:2396)
at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1662)
at org.springframework.http.converter.json.MappingJacksonHttpMessageConverter.readInternal(MappingJacksonHttpMessageConverter.java:135)
at org.springframework.http.converter.AbstractHttpMessageConverter.read(AbstractHttpMessageConverter.java:154)
at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:74)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:446)
Trying to coerce the String object types also yield the same result. It seems like there is some kind of limit to the length.
Does anyone know what’s the reason for this? Have tried looking around but couldn’t find any answer.
I believe the Jackson support for both RestTemplate and MVC (which use the same converter if I recall) will only work for Objects, Collections, and Maps.
Thus you should probably do something like:
Which will return a response like:
That is use objects, collections, and maps over literals like numbers, and strings. It also makes sense because:
350094078853290372226929324000742644466642262681952226409886256271736198431807728267526406481556587992703721192341876290504971375383029475460844121232586227913173796191951197042895656298303777249177945550375699884594977303136892333029758596862673993846972456155271723432095094859391751714939658055297007851105568438549148984628844489662466256443824528144049402449678472642642Is not valid JSON. The reason it appears to work for MVC is that a different
Converterthan the Jackson one is doing the conversion.You should also know that BigInteger is not support in JSON in general (whether its in an Object or not). I am pretty sure Jackson will convert it to String but you should be aware of that also.
I would’t use BigInteger for a token (if thats what your using it for) but instead use base64 encoded string which will be smaller and probably safer.