Skip to main content
Calling a rest service with spring rest + json jackson Presenting to you step by step a tutorial about the use of some apis that leverage the logic for calling to a rest service and also the json parting and marshalling process
First Step Importing required libs:

You will need to import spring for droid there are the files:
spring-android-auth-1.0.0.M4.jar
spring-android-core-1.0.0.M4.jar
spring-android-rest-template-1.0.0.M4.jar


You will need to import jackson lib there is the file:
jackson-all-1.9.4.jar

Second Step : Declaring variables
You will need these three variables to do the trick. I will recomend to declare them as fields and instantiate them later on class constructor.

RestTemplate restTemplate = new RestTemplate();
ObjectMapper objectMapper = new ObjectMapper();
JsonFactory jsonFactory = new JsonFactory();


Third step: Calling rest service:

Just call this method of rest template and the api will call the service and will encapsulate all the stream stuff and present you the response as a string file in this example.

String url = "yourjsonrestserviceurl");
String result = restTemplate.getForObject(url, String.class);


Fourth Step: Creating json parser and marshalling the json result into an object

JsonParser jp = jsonFactory.createJsonParser(result);
MappingIterator list = objectMapper.readValues(jp, YourBean[].class);


Finally Some recomendations:
You should get some errors depending of the format of your json. Here are some workarounds:

to map a field with different names:
Add anotation @JsonProperty("yourJSONfield") to the POJO field you want to map.
To ignore a POJO field on marshalling process: @JsonIgnore

Comments

Popular posts from this blog

Removing duplicates with awk

To remove duplicates in linux you can use awk command as follows:  awk -F- '!seen[$1]++' file.txt > output.txt where: -F is paraeter to deternime separator used to define columns and in ths case the dash 8-) is the sparator used '!seen[$1]++' is the expression used for the line in order to be printed > is the instruction to send command output o a file

wcs query to fetch shipping cost by store

In websphere commerce 7 model you can use the folloging query to fetch the shipping cost of catalog entries. This query could also be useful for newer versions. select cr.calrange_id,cc.calcode_id, c.partnumber,j.code,j.description,value from catentry c left outer join CATENCALCD cd on c.catentry_id=cd.catentry_id left outer join calcode cc on  cd.calcode_id=cc.calcode_id left outer join calrule r on cc.calcode_id=r.calcode_id left outer join CRULESCALE s on r.calrule_id=s.calrule_id left outer join CALRANGE cr on s.calscale_id=cr.calscale_id left outer join CALRLOOKUP cl on cr.CALRANGE_id=cl.CALRANGE_id left outer join SHPJCRULE sr on sr.calrule_id=r.calrule_id left outer join JURSTGROUP jg on jg.jurstgroup_id=sr.jurstgroup_id left outer join JURSTGPREL jgr on jgr.jurstgroup_id=jg.jurstgroup_id left outer join JURST j on j.jurst_id=jgr.jurst_id where (partnumber like 'partnumber% ) and catenttype_id in ('ItemBean','PackageBean') and cc.storeent_id=stor...

Diferencias del Metodo equals() y ==

Por ejemplo, el tipo de datos int, en Java, es un tipo de dato de los llamados primitivos (más o menos como c++), con lo cuál puedes utilizar los operadores lógicos y aritméticos de la forma habitual. En tu caso puedes utilizar el operador == de la manera normal. Sin embargo, Integer, no es un tipo de dato primitivo. Es una clase!!. Entoces cuando tú tienes un objeto del tipo Integer NO estás manejando su valor, sino un puntero a tu objeto Integer. Porque en Java todo son punteros (menos los tipos de datos primitivos). Así que cuando utilizas el operador == no estás comparando el valor de los integer, sino los punteros (las direcciones que apuntan al objeto). De modo que para objetos estás obligado a utilizar el método equals().