Criando seu primeiro serviço Java-First com JAXWS e Maven
Autor: zero-2 | Publicado: 2013-01-22T15:09:00-08:00

Neste artigo, criaremos nosso primeiro serviço utilizando a API JaxWS. Para implementar os exercícios mencionados aqui, é necessário fazer algumas configurações no seu ambiente de desenvolvimento (seu ambiente necessitará de acesso a web).

A primeira modificação necessária, é instalar o plugin para adicionar suporte a Maven no eclipse (sim, utilizaremos o eclipse como IDE). Para efetuar esta instalação, abra o eclipse market e instale o plugin M2E. Outro ponto importante é atualizar sua biblioteca Jaxb (se estiver utilizando o JDK 1.6). Para executar esta atividade, copie a biblioteca do jaxb 2.2.0 ou superior para a pasta endorsed da JRE em execução (no meu caso, copiei para &lt;i&gt;/usr/lib/jvm/jdk1.6.0_37/jre/lib/endorsed&lt;/i&gt; (caso a pasta endorsed não exista, você deverá criá-la).

Lembre-se também que será necessário ter o servlet container Tomcat devidamente configurado e executando em sua máquina (não coberto pelo artigo).

Crie um projeto do tipo Maven (File -&gt; New -&gt; Project -&gt; Maven -&gt; Maven Project), e selecione a opção "Create a simple project (skip archetype selection)" e preencha as informações básicas do projeto (seção Artifact) de acordo com os dados disponibilizados abaixo:

Group Id: br.com.ws.exemploArtifact Id: helloplanet-jaxwsVersion: 1.0.0Packaging: war

O projeto é criado e automaticamente é adicionado um arquivo chamado pom.xml. Este arquivo configura o processo de compilação e construção do pacote final de software. Precisamos alterá-lo para compilar o projeto de acordo com as nossas necessidades (gerar o serviço e a estrutura de um projeto war). Adicione um source folder no projeto (Clicar com o botão direito sobre o projeto -&gt; New -&gt; Source Folder), seguindo a estrutura main/src e nomeie como "webresources". Altere seu pom.xml de acordo com a estrutura abaixo:

&lt;project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"&gt;&nbsp; &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;&nbsp; &lt;groupId&gt;br.com.ws.exemplo&lt;/groupId&gt;&nbsp; &lt;artifactId&gt;helloplanet-jaxws&lt;/artifactId&gt;&nbsp; &lt;version&gt;1.0.0&lt;/version&gt;&nbsp; &lt;packaging&gt;war&lt;/packaging&gt;&nbsp; &lt;dependencies&gt;&nbsp; &nbsp;&nbsp;&nbsp; &lt;dependency&gt;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;groupId&gt;com.sun.xml.ws&lt;/groupId&gt;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;artifactId&gt;jaxws-rt&lt;/artifactId&gt;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;version&gt;${jaxb.version}&lt;/version&gt;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;scope&gt;compile&lt;/scope&gt;&nbsp; &nbsp;&nbsp;&nbsp; &lt;/dependency&gt;&nbsp; &lt;/dependencies&gt;&nbsp; &lt;properties&gt;&nbsp; &nbsp;&nbsp;&nbsp; &lt;jaxb.version&gt;2.2.7&lt;/jaxb.version&gt;&nbsp; &lt;/properties&gt;&nbsp; &lt;build&gt;&nbsp; &nbsp;&nbsp;&nbsp; &lt;plugins&gt;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;plugin&gt;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;artifactId&gt;maven-war-plugin&lt;/artifactId&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;version&gt;2.1.1&lt;/version&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;configuration&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;webResources&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;resource&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;directory&gt;${basedir}/src/main/webresources&lt;/directory&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;/resource&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;/webResources&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;warName&gt;helloplanet-jaxws&lt;/warName&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;/configuration&gt;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;/plugin&gt;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;plugin&gt;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &lt;groupId&gt;org.codehaus.mojo&lt;/groupId&gt;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &lt;artifactId&gt;jaxws-maven-plugin&lt;/artifactId&gt;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &lt;version&gt;1.11&lt;/version&gt;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &lt;executions&gt;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;execution&gt;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;goals&gt;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;goal&gt;wsgen&lt;/goal&gt;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/goals&gt;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;configuration&gt;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;sei&gt;br.com.exemplo.ws.impl.HelloPlanetImpl&lt;/sei&gt;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;genWsdl&gt;true&lt;/genWsdl&gt;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;keep&gt;true&lt;/keep&gt;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/configuration&gt;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;/execution&gt;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &lt;/executions&gt;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;/plugin&gt;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;plugin&gt;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &lt;groupId&gt;org.apache.maven.plugin&lt;/groupId&gt;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &lt;version&gt;2.5.1&lt;/version&gt;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &lt;configuration&gt;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;source&gt;1.6&lt;/source&gt;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;target&gt;1.6&lt;/target&gt;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &lt;/configuration&gt;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;/plugin&gt;&nbsp; &nbsp;&nbsp;&nbsp; &lt;/plugins&gt;&nbsp; &lt;/build&gt;&nbsp; &lt;/project&gt;
É importante explicar melhor algumas partes do projeto, como a seguinte:

&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;plugin&gt;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;artifactId&gt;maven-war-plugin&lt;/artifactId&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;version&gt;2.1.1&lt;/version&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;configuration&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;webResources&gt;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;!-- configura a pasta webresources como a pasta de recursos do projeto web --&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;resource&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;directory&gt;${basedir}/src/main/webresources&lt;/directory&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;/resource&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;/webResources&gt;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;!-- define o nome do web archive (war) que será gerado --&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;warName&gt;helloplanet-jaxws&lt;/warName&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;/configuration&gt;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;/plugin&gt;

O trecho abaixo também merece uma atenção especial, pois configura a execução do plugin de geração do serviço: 

&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;plugin&gt;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &lt;groupId&gt;org.codehaus.mojo&lt;/groupId&gt;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &lt;artifactId&gt;jaxws-maven-plugin&lt;/artifactId&gt;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &lt;version&gt;1.11&lt;/version&gt;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &lt;executions&gt;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;execution&gt;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;goals&gt;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;goal&gt;wsgen&lt;/goal&gt;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/goals&gt;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;configuration&gt;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;sei&gt;br.com.exemplo.ws.impl.HelloPlanetImpl&lt;/sei&gt;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;genWsdl&gt;true&lt;/genWsdl&gt;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;keep&gt;true&lt;/keep&gt;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/configuration&gt;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;/execution&gt;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &lt;/executions&gt;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;/plugin&gt;

Agora, vamos criar a pasta WEB-INF e colocar nosso arquivo web.xml dentro. Esta estrutura é necessária para aplicações web em java. Clique com o botão direito sobre a pasta webresources e adicione uma pasta denominada WEB-INF. Adicione um novo arquivo xml nesta pasta e nomeie este arquivo como web.xml. O conteúdo do web.xml deve ser o seguinte:

&lt;?xml version="1.0" encoding="UTF-8"?&gt;&lt;web-app xmlns="http://java.sun.com/xml/ns/j2ee"&nbsp;&nbsp;&nbsp; xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&nbsp;&nbsp;&nbsp; xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"&nbsp;&nbsp;&nbsp; version="2.4"&gt;&nbsp;&nbsp;&nbsp; &lt;display-name&gt;HelloPlanetJAXWS Application&lt;/display-name&gt;&nbsp;&nbsp;&nbsp; &lt;description&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; This is a simple web application with a source code organization&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; based on the Java web-apps standard.&nbsp;&nbsp;&nbsp; &lt;/description&gt;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;listener&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;listener-class&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; com.sun.xml.ws.transport.http.servlet.WSServletContextListener&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/listener-class&gt;&nbsp;&nbsp;&nbsp; &lt;/listener&gt;&nbsp;&nbsp;&nbsp; &lt;servlet&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;servlet-name&gt;HelloPlanetJAXWS&lt;/servlet-name&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;servlet-class&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; com.sun.xml.ws.transport.http.servlet.WSServlet&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/servlet-class&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;&nbsp;&nbsp;&nbsp; &lt;/servlet&gt;&nbsp;&nbsp;&nbsp; &lt;servlet-mapping&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;servlet-name&gt;HelloPlanetJAXWS&lt;/servlet-name&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;url-pattern&gt;/hello&lt;/url-pattern&gt;&nbsp;&nbsp;&nbsp; &lt;/servlet-mapping&gt;&nbsp;&nbsp;&nbsp; &lt;session-config&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;session-timeout&gt;120&lt;/session-timeout&gt;&nbsp;&nbsp;&nbsp; &lt;/session-config&gt;&lt;/web-app&gt;

Perceba que neste arquivo configuramos o servlet do JaxWS para para carregar nosso serviço. Agora que temos o webxml devidamente configurado, devemos configurar nosso projeto. Dentro desta mesma pasta (WEB-INF), adicione o arquivo sun-jaxws.xml para configuração do serviço. Este arquivo deve conter o seguinte conteúdo:

&lt;?xml version="1.0" encoding="UTF-8"?&gt;&lt;endpoints xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime" version="2.0"&gt;&nbsp; &lt;endpoint name="HelloPlanetJAXWSEndPoint" implementation="br.com.exemplo.ws.impl.HelloPlanetImpl" url-pattern="/hello"/&gt;&lt;/endpoints&gt;

Repare que a url do segundo arquivo (sun-jaxws.xml) é a mesma utilizada no primeiro (web.xml). Agora que já configuramos todo o projeto, vamos ao código. Primeiro vamos criar a interface do serviço. Para isso, clique com o botão direito sobre a pasta "src/main/java" e selecione New -&gt; Interface.

No pacote, adicione br.com.exemplo.ws e dê o nome HelloPlanet. Insira o seguinte código para a interface criada:

package br.com.exemplo.ws;import javax.jws.WebMethod;import javax.jws.WebParam;import javax.jws.WebResult;import javax.jws.WebService;import javax.xml.bind.annotation.XmlElement;import br.com.exemplo.ws.domain.Planet;@WebService(name="HelloPlanet", targetNamespace="http://www.helloplanet.com.br/jaxws")public interface HelloPlanet {&nbsp;&nbsp;&nbsp; @WebMethod(operationName="SayHello", action="http://www.helloplanet.com.br/jaxws/sayHello")&nbsp;&nbsp;&nbsp; @WebResult(name="Greeting", targetNamespace="http://www.helloplanet.com.br/jaxws/sayhello/result")&nbsp;&nbsp;&nbsp; public String sayHello(@WebParam(name="PlanetName", targetNamespace="http://www.helloplanet.com.br/jaxws/domain/planet") @XmlElement(required=true, nillable=false) Planet planet);}

Repare que a nossa interface renomeia o método, parâmetro e resultado retornado. Outro ponto importante é a anotação @XmlElement que efetuará a validação do parâmetro para o serviço (necessário ter sobrescrito a jaxb api conforme descrito no começo do artigo). Agora vamos criar o código da nossa classe. Para isso, clique com o botão direito sobre a pasta "src/main/java" e selecione New -&gt; Class, configure o package como "br.com.exemplo.ws.impl", nomeie a classe como HelloPlanetImpl e adicione o seguinte código:

package br.com.exemplo.ws.impl;import javax.jws.WebService;import com.sun.xml.ws.developer.SchemaValidation;import br.com.exemplo.ws.HelloPlanet;import br.com.exemplo.ws.domain.Planet;@SchemaValidation@WebService(endpointInterface="br.com.exemplo.ws.HelloPlanet", portName="HelloPlanetPort",&nbsp; serviceName="HelloPlanet")public class HelloPlanetImpl implements HelloPlanet {&nbsp;&nbsp;&nbsp; public String sayHello(Planet planet) {&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; return "Hello " + planet.getName() + "! Your id is: " + planet.getId() + " and your distance is: " + planet.getDistance();&nbsp;&nbsp;&nbsp; }}

Neste ponto, você ainda deve estar vendo um erro no seu projeto. A classe Planet, que é recebida como parâmetro pelo método (operação do serviço) ainda não existe, então vamos criá-la. Para isso, clique com o botão direito sobre a pasta "src/main/java" e selecione New -&gt; Class, configure o package como "br.com.exemplo.ws.domain", nomeie a classe como Planet e adicione o seguinte código:

package br.com.exemplo.ws.domain;import javax.xml.bind.annotation.XmlAccessType;import javax.xml.bind.annotation.XmlAccessorType;import javax.xml.bind.annotation.XmlElement;import javax.xml.bind.annotation.XmlRootElement;@XmlRootElement(name="Planet", namespace="http://www.helloplanet.com.br/jaxws/domain/planet")@XmlAccessorType(XmlAccessType.FIELD)public class Planet {&nbsp;&nbsp;&nbsp; @XmlElement(name="ID", required=true, nillable=true)&nbsp;&nbsp;&nbsp; private Integer id;&nbsp;&nbsp;&nbsp; @XmlElement(name="Name", required=false, nillable=false)&nbsp;&nbsp;&nbsp; private String name;&nbsp;&nbsp;&nbsp; @XmlElement(name="Distance", required=true, nillable=false)&nbsp;&nbsp;&nbsp; private Double distance;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; public Integer getId() {&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; return id;&nbsp;&nbsp;&nbsp; }&nbsp;&nbsp;&nbsp; public void setId(Integer id) {&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; this.id = id;&nbsp;&nbsp;&nbsp; }&nbsp;&nbsp;&nbsp; public String getName() {&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; return name;&nbsp;&nbsp;&nbsp; }&nbsp;&nbsp;&nbsp; public void setName(String name) {&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; this.name = name;&nbsp;&nbsp;&nbsp; }&nbsp;&nbsp;&nbsp; public Double getDistance() {&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; return distance;&nbsp;&nbsp;&nbsp; }&nbsp;&nbsp;&nbsp; public void setDistance(Double distance) {&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; this.distance = distance;&nbsp;&nbsp;&nbsp; }&nbsp;&nbsp;&nbsp; }

Pronto! Agora vamos compilar o projeto. Para isso, clique com o botão direto sobre o arquivo pom.xml e selecione "Run as -&gt; Maven Install". Aguarde o maven baixar as dependências e compilar o projeto. Ao final do processo de compilação você deverá ver uma mensagem semelhante a esta:

[INFO] ------------------------------------------------------------------------[INFO] BUILD SUCCESS[INFO] ------------------------------------------------------------------------[INFO] Total time: 5.439s[INFO] Finished at: Wed Dec 12 20:10:13 BRST 2012[INFO] Final Memory: 10M/25M[INFO] ------------------------------------------------------------------------

Verifique a pasta target do seu projeto, e dentro, deverá existir o arquivo helloplanet-jaxws.war. Este arquivo será jogado dentro da pasta webapps do tomcat. Após copiar o arquivo, acesse a URL:

http://localhost:8080/helloplanet-jaxws/hello?wsdl

E você verá o wsdl pertinente ao seu serviço. Parabéns, você criou seu primeiro serviço utilizando JaxWS e ele está disponível em: 

http://localhost:8080/helloplanet-jaxws/hello

Caso tenha percebido, o @XmlElement não gerou a validação corretamente para o serviço (xml retirado de exemplo de consumo utilizando SoapUI). 

&lt;soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:jax="http://www.helloplanet.com.br/jaxws" xmlns:plan="http://www.helloplanet.com.br/jaxws/domain/planet"&gt;&nbsp;&nbsp; &lt;soapenv:Header/&gt;&nbsp;&nbsp; &lt;soapenv:Body&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;jax:SayHello&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;!--Optional:--&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;plan:PlanetName&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;ID&gt;?&lt;/ID&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;!--Optional:--&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;Name&gt;?&lt;/Name&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;Distance&gt;?&lt;/Distance&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/plan:PlanetName&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/jax:SayHello&gt;&nbsp;&nbsp; &lt;/soapenv:Body&gt;&lt;/soapenv:Envelope&gt;

Repare que o parâmetro PlanetName está demarcado como opcional (trecho retirado do contrato gerado) enquanto que a validação foi respeitada para o parâmetro planetId:

&lt;xs:element ref="ns1:PlanetName" minOccurs="0"/&gt;

Este é um bug do JaxWS (a validação através de @XmlElement é respeitada para tipos primitivos da linguagem mas não para objetos) e a para sanar o problema, podemos utilizar CXF para a construção do nosso serviço, como veremos no próximo post.