<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Rafael Liu Blog &#187; RESTEasy</title>
	<atom:link href="http://rafaelliu.net/tag/resteasy/feed/" rel="self" type="application/rss+xml" />
	<link>http://rafaelliu.net</link>
	<description>Café, pinguins e outras coisas</description>
	<lastBuildDate>Mon, 05 Dec 2011 19:08:47 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.3</generator>
		<item>
		<title>REST com RESTEasy &#8211; JAXB e RESTEasy Client</title>
		<link>http://rafaelliu.net/2009/02/05/rest-com-resteasy-jaxb-e-resteasy-client/</link>
		<comments>http://rafaelliu.net/2009/02/05/rest-com-resteasy-jaxb-e-resteasy-client/#comments</comments>
		<pubDate>Thu, 05 Feb 2009 19:27:09 +0000</pubDate>
		<dc:creator>rafaelliu</dc:creator>
				<category><![CDATA[JBoss]]></category>
		<category><![CDATA[SOA]]></category>
		<category><![CDATA[jaxb]]></category>
		<category><![CDATA[REST]]></category>
		<category><![CDATA[RESTEasy]]></category>

		<guid isPermaLink="false">http://rafaelliu.net/?p=68</guid>
		<description><![CDATA[Uma ótima apresentação sobre REST me fez querer escrever um pouco sobre. Mais especificamente sobre o RESTEasy, implemetação certificada JAX-RS da JBoss. No projeto que fiz para certificação JBoss Certified SOA Developer o usei bastante e quero falar de algumas interessantes funcionalidades dele. Primeiro algumas considerações sobre REST: RESTEasy é um framework para criação de [...]]]></description>
			<content:encoded><![CDATA[<p>Uma <a title="Apresentação REST" href="http://www.infoq.com/news/2009/01/REST-Stefan-Tilkov" target="_blank">ótima apresentação sobre REST</a> me fez querer escrever um pouco sobre. Mais especificamente sobre o <a title="Projeto RESTEasy" href="http://jboss.org/resteasy/" target="_blank">RESTEasy</a>, implemetação certificada JAX-RS da JBoss. No projeto que fiz para certificação <em>JBoss Certified SOA Developer</em> o usei bastante e quero falar de algumas interessantes funcionalidades dele. Primeiro algumas considerações sobre REST:</p>
<ol>
<li>RESTEasy é um framework para criação de <span style="text-decoration: underline;"><em>RESTful Web Services</em></span>, o que em algum senso já é uma desvirtuação de REST (tal como Roy Fielding definiu), que trata de recursos. O que tem haver um serviço RESTful que loga o usuário? (O que aliás abre uma <span style="text-decoration: line-through;">sedutora</span> brecha para quebrar outro conceito REST: o de statelessness) De agora em diante irei usar REST e RESTful Web Services indistintamente.</li>
<li>REST não é uma alternativa a SOA! Na verdade podemos usar REST para implementar SOA com WOA. Confuso? REST é uma alternativa ao WS-*, podendo ser usado no lugar dele para implementar SOA. A implementação RESTful de SOA é chamada WOA, ou ROA (veja a <a title="TLAs" href="/?page_id=64" target="_blank">página de TLAs</a>), que é um subconjunto de SOA.</li>
</ol>
<h4>JAXB binding</h4>
<p>RESTEasy possui suporte a marshalling/unmarshalling de classes usando JAXB. Sou péssimo para dar exemplos mas vamos lá&#8230;:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">@Path<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/&quot;</span><span style="color: #009900;">&#41;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Servico <span style="color: #009900;">&#123;</span>
&nbsp;
	@GET @Path<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/tempo&quot;</span><span style="color: #009900;">&#41;</span>
	@Produces<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;text/xml&quot;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #000000; font-weight: bold;">public</span> Tempo getTempo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> Tempo<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Parcialmente nublado&quot;</span>, <span style="color: #cc66cc;">20.3</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@POST @Path<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/literal&quot;</span><span style="color: #009900;">&#41;</span>
	@Consumes<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;text/xml&quot;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setTempo<span style="color: #009900;">&#40;</span>Tempo tempo<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">format</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Descrição: %s%n&quot;</span>, tempo.<span style="color: #006633;">getDescricao</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">format</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Temperatura: %d%n&quot;</span>, tempo.<span style="color: #006633;">getTemperatura</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// classe anotada com JAXB</span>
@XmlRootElement
<span style="color: #000000; font-weight: bold;">class</span> Tempo <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> descricao<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Double</span> temperatura<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> Tempo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> Tempo<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> nome, <span style="color: #003399;">Double</span> preco<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">descricao</span> <span style="color: #339933;">=</span> nome<span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">temperatura</span> <span style="color: #339933;">=</span> preco<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getDescricao<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> descricao<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setDescricao<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> nome<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">descricao</span> <span style="color: #339933;">=</span> nome<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Double</span> getTemperatura<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> temperatura<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setTemperatura<span style="color: #009900;">&#40;</span><span style="color: #003399;">Double</span> preco<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">temperatura</span> <span style="color: #339933;">=</span> preco<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Na invocação de getTempo() RESTEasy vai ver que o tipo de retorno é o objeto Tempo e que ele está anotado com @XmlRootElement e vai fazer um marshalling dele. A anotação @Produces(&#8220;text/xml&#8221;) diz que a representação gerada deve ser XML. Quer que seja em JSON? Basta mudar para @Produces(&#8220;application/json&#8221;)!</p>
<p>Do mesmo jeito funciona o setTempo(). O parâmetro passados sem nenhuma anotação é interpretado como o corpo da requisição HTTP, que no caso é um objeto.</p>
<h4>RESTEasy Client</h4>
<p>Algo muito interessante que não é da especificação JAX-RS é o suporte a clients. Esses clients são proxies que transformam invocações Java em requisições HTTP. Para isso devemos criar uma interface com as mesmas assinaturas do nosso serviço e anotá-las:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Main <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">static</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// precisa ser chamo uma única vez para registrar providers RESTEasy, scanear classes, etc</span>
		 RegisterBuiltin.<span style="color: #006633;">register</span><span style="color: #009900;">&#40;</span>ResteasyProviderFactory.<span style="color: #006633;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// precisa ser chamado uma única vez para</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> args<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
 		 IServico client <span style="color: #339933;">=</span> ProxyFactory.<span style="color: #006633;">create</span><span style="color: #009900;">&#40;</span>IServico.<span style="color: #000000; font-weight: bold;">class</span>, <span style="color: #0000ff;">&quot;http://localhost:8080/&amp;lt;Context&amp;gt;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		 Tempo tempo <span style="color: #339933;">=</span> client.<span style="color: #006633;">getTempo</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		 tempo.<span style="color: #006633;">setTemperatura</span><span style="color: #009900;">&#40;</span>tempo.<span style="color: #006633;">getTemperatura</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #cc66cc;">5.0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		 client.<span style="color: #006633;">setTempo</span><span style="color: #009900;">&#40;</span>tempo<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">interface</span> IServico <span style="color: #009900;">&#123;</span>
&nbsp;
	@GET @Path<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/tempo&quot;</span><span style="color: #009900;">&#41;</span>
	@Produces<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;text/xml&quot;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">abstract</span> Tempo getTempo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	@POST @Path<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/literal&quot;</span><span style="color: #009900;">&#41;</span>
	@Consumes<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;text/xml&quot;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">abstract</span> <span style="color: #000066; font-weight: bold;">void</span> setTempo<span style="color: #009900;">&#40;</span>Tempo tempo<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// classe anotada com JAXB</span>
@XmlRootElement
<span style="color: #000000; font-weight: bold;">class</span> Tempo <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> descricao<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Double</span> temperatura<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> Tempo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> Tempo<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> nome, <span style="color: #003399;">Double</span> preco<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">descricao</span> <span style="color: #339933;">=</span> nome<span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">temperatura</span> <span style="color: #339933;">=</span> preco<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getDescricao<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> descricao<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setDescricao<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> nome<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">descricao</span> <span style="color: #339933;">=</span> nome<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Double</span> getTemperatura<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> temperatura<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setTemperatura<span style="color: #009900;">&#40;</span><span style="color: #003399;">Double</span> preco<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">temperatura</span> <span style="color: #339933;">=</span> preco<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Agora você deve ver no console:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">Descrição: Parcialmente nublado
Temperatura: 34.000000</pre></div></div>

<p>Precisamos a classe Tempo também apenas porque estamos usando JAXB. Tudo transparente, muito fácil agora fazer teste unitários de serviços RESTful!</p>
]]></content:encoded>
			<wfw:commentRss>http://rafaelliu.net/2009/02/05/rest-com-resteasy-jaxb-e-resteasy-client/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

