# Web Services com JAX-WS

![](/files/-MJrYULIR7aCpz6ym9D8)

A Java XML API for Web Services (JAX-WS) é uma biblioteca de Web Services para Java, que permite implementar serviços baseados nas normas XSD, WSDL e SOAP. A JAX-WS define o mapeamento de WSDL para Java e vice-versa. Com a JAX-WS é possível implementar Web Services partindo de um contrato WSDL ou partindo de código Java. Com a JAX-WS é também possível criar código cliente de invocação de Web Services.

![Modelo Cliente / Servidor JAX-WS em SOAP ](/files/-MJrW_xclFJpBrMM6exT)

**Implementando o Endpoint do Serviço**

![Estrutura Endpoint no Eclipse](/files/-MJrX0Hb4P1lB8zyf4IR)

Para implementação da classe de serviço utilizaremos as anotações *@WebService* e *@WebMethod* do pacote javax.

```java
package service;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;

@WebService
public class WsServidor {

	@WebMethod
	public String exibirVersao() {

		return "Web Service com JAX-WS, versão 1.0";
	}
	@WebMethod
	public String retornarString(@WebParam(name = "msg") String msg) {

		return "echo :: " + msg;
	}
	@WebMethod
	public Double retornarSoma(@WebParam(name = "valor1") Double x,
			@WebParam(name = "valor2") Double y) {

		return x + y;
	}
}
```

**@WebService :** representa uma interface ou classe de serviço.

**@WebMethod:** representa os métodos de uma interface de serviço.

**@WebParam:** representa os parâmetros de um método.

**Gerando os artefatos do Serviço**

Para gerar os artefatos do Serviço utilizaremos a ferramenta **wsgen**. Execute os comandos do wsgen apontado pro diretório raiz do projeto.

```bash
wsgen -s src -d src -cp build\classes -keep service.WsServidor
```

Para gerar com o WSDL, ServicePort e ServiceName use o comando a seguir, digite tudo em uma única linha

```bash
wsgen -s src -d src -cp build\classes -wsdl -r wsdl -keep 
-portname "{http://jaxWsServidor/}JaxWsServidorPort" 
-servicename "{http://jaxWsServidor/}JxWsServidorService" 
service.WsServidor
```

Depois de gerados os artefatos com o wsgen o projeto deverá ficar com seguinte estrutura:

![](/files/-MJrXxNh9mN7Uqr91IDe)

**Publicando o EndPoint do Serviço**

Para publicar o Serviço, criaremos uma classe com o publicador do endpoint.

```java
package service;

import javax.xml.ws.Endpoint;

public class PublicadorWsServidor {

	public static void main(String[] args) {		
		WsServidor servidor = new WsServidor();
		Endpoint.publish("http://localhost:8080/WsServidor/service", servidor);
		System.out.println("Servidor Publicado: http://localhost:8080/WsServidor/service");		
	}
}
```

&#x20;Para visualizar execute main para publicar o Serviço e digite em seu navegador: [*http://localhost:8080/WsServidor/service*](http://localhost:8080/WsServidor/service)**.**


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://pauloricmarinho.gitbook.io/desenvolvimento/webservices/gerando-webservices-com-jax-ws.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
