A brief example of how to use the Spring Jackson library to consume a JSON returned by a REST service. The class below is a Singleton that initializes the RestTemple one time only.
import java.util.ArrayList; import java.util.List; import org.springframework.http.MediaType; import org.springframework.http.client.CommonsClientHttpRequestFactory; import org.springframework.http.converter.HttpMessageConverter; import org.springframework.http.converter.json.MappingJacksonHttpMessageConverter; import org.springframework.web.client.RestTemplate; public class RestJsonTest { private static RestJsonTest instance; private RestTemplate restTemplate; private String url ="http://rest.service.url"; public static RestJsonTest getInstance() { if (instance == null) { instance = new RestJsonTest(); } return instance; } private RestJsonTest() { // Setup the RestTemplate configuration. restTemplate = new RestTemplate(); restTemplate.setRequestFactory(new CommonsClientHttpRequestFactory()); List<HttpMessageConverter<?>> messageConverterList = restTemplate.getMessageConverters(); // Set HTTP Message converter using a JSON implementation. MappingJacksonHttpMessageConverter jsonMessageConverter = new MappingJacksonHttpMessageConverter(); // Add supported media type returned by BI API. List<MediaType> supportedMediaTypes = new ArrayList<MediaType>(); supportedMediaTypes.add(new MediaType("text", "plain")); supportedMediaTypes.add(new MediaType("application", "json")); jsonMessageConverter.setSupportedMediaTypes(supportedMediaTypes); messageConverterList.add(jsonMessageConverter); restTemplate.setMessageConverters(messageConverterList); } public SearchResults searchResults() { return restTemplate.getForObject(url, SearchResults.class); } public static void main(String[] args) { RestJsonTest jsonTest = RestJsonTest.getInstance(); SearchResults results = jsonTest.searchResults(); } }The mapping of the JSON to Java classes can be done via annotations like it's shown here:
package com.bodybuilding.api.commerce.clientservice; import java.util.List; import org.codehaus.jackson.annotate.JsonIgnoreProperties; import org.codehaus.jackson.annotate.JsonProperty; /** *{ * "search_keywords":"Social Networks", * "total_time":200, * "results":{ * "result_01":{ * "url":"http://www.facebook.com", * "rank": "1" * }, * "result_02":{ * "url":"http://www.twitter.com", * "rank": "2" * } * } * } */ @JsonIgnoreProperties(ignoreUnknown=true) public class SearchResults { @JsonProperty("search_keywords") private String keywords; @JsonProperty("total_time") private long totalTime; @JsonProperty("results") private List<SearchResult> results; public String getKeywords() { return keywords; } public void setKeywords(String keywords) { this.keywords = keywords; } public long getTotalTime() { return totalTime; } public void setTotalTime(long totalTime) { this.totalTime = totalTime; } public List<SearchResult> getResults() { return results; } public void setResults(List<SearchResult> results) { this.results = results; } }
import org.codehaus.jackson.annotate.JsonIgnoreProperties; import org.codehaus.jackson.annotate.JsonProperty; @JsonIgnoreProperties(ignoreUnknown=true) public class SearchResult { @JsonProperty("url") private String url; @JsonProperty("rank") private int rank; public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public int getRank() { return rank; } public void setRank(int rank) { this.rank = rank; } }The RestTemplate bean can be also configure with Spring injection:
<bean id="restTemplate" class="org.springframework.web.client.RestTemplate"> <property name="requestFactory"> <bean id="clientHttpRequestFactory" class="org.springframework.http.client.CommonsClientHttpRequestFactory" /> </property> <property name="messageConverters"> <list> <bean id="jsonMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <bean id="jsonMediaTypeTextPlain" class="org.springframework.http.MediaType"> <constructor-arg value="text"/> <constructor-arg value="plain"/> </bean> <bean id="jsonMediaTypeApplicationJson" class="org.springframework.http.MediaType"> <constructor-arg value="application"/> <constructor-arg value="json"/> </bean> </list> </property> </bean> </list> </property> </bean>
Nice and good article. It is very useful for me to learn and understand easily. Thanks for sharing your valuable information and time. Please keep updatingmulesoft online training
ReplyDeleteThis post is really nice and informative. The explanation given is really comprehensive and informative.
ReplyDeletemulesoft 4 training
instagram takipçi satın al
ReplyDeleteinstagram takipçi satın al
aşk kitapları
tiktok takipçi satın al
instagram beğeni satın al
youtube abone satın al
twitter takipçi satın al
tiktok beğeni satın al
tiktok izlenme satın al
twitter takipçi satın al
tiktok takipçi satın al
youtube abone satın al
tiktok beğeni satın al
instagram beğeni satın al
trend topic satın al
trend topic satın al
youtube abone satın al
instagram takipçi satın al
beğeni satın al
tiktok izlenme satın al
sms onay
youtube izlenme satın al
tiktok beğeni satın al
sms onay
sms onay
perde modelleri
instagram takipçi satın al
takipçi satın al
tiktok jeton hilesi
instagram takipçi satın al pubg uc satın al
sultanbet
marsbahis
betboo
betboo
betboo
I appreciate this piece of useful information,Thank You .I want to share about Best MicroNutrients Company in India
ReplyDeleteperde modelleri
ReplyDeletesms onay
mobil ödeme bozdurma
nft nasıl alınır
ankara evden eve nakliyat
trafik sigortası
DEDEKTOR
web sitesi kurma
aşk kitapları
Smm Panel
ReplyDeleteSMM PANEL
iş ilanları
instagram takipçi satın al
hirdavatciburada.com
BEYAZESYATEKNİKSERVİSİ.COM.TR
servis
tiktok jeton hilesi