RESTeasy to make a REST Call

Previously I wrote an article to use Jersey to make a REST call. Afterwards, we changed to use RESTeasy to do that. The reason we changed that doesn't convince me, because we decided to use JSON instead of XML. Eventually we didn't choose JSON because we handle parsing on sever side not client side. It's very fast to use javascript to parse JSON object, but not suitable for our case.

Anyway, our standalone app and portlet should be consistent. Since standalone app already uses RESTeasy, I need to change portlet to use the same technique.

public static Article getAtricleByUrl(String url){
		Article article = null;
		try{
			ClientRequest request = new ClientRequest(url);
			request.accept(MediaType.APPLICATION_XML);
			ClientResponse<String> response = request.get(String.class);
			BufferedReader br = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(response.getEntity().getBytes())));
			JAXBContext context = JAXBContext.newInstance(Article.class);
			Unmarshaller jaxbUnmarshaller = context.createUnmarshaller();
			article = (Article) jaxbUnmarshaller.unmarshal(br);
		}catch(Exception e){
			logger.error("REST Call to NewsCred failed!");
			logger.error(e.getMessage());
		}
		return article;
	}

It's very easy to do that, right. The difficult thing is to write POJO model to match responded XML.

Added dependency into your pom.xml file

<dependency>
	<groupId>org.jboss.resteasy</groupId>
	<artifactId>resteasy-jaxrs</artifactId>
	<version>2.3.0.GA</version>
</dependency>

Here is the link to use Jersey to make a REST call. http://blog.csdn.net/smile_juan/article/details/8515733

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章