# Web Services com Spring e Apache CXF

Criação de um Web Service básico utilizando o Framework Spring e o Apache CXF. O projeto de exemplo foi criado com a seguinte estrutura:

![](/files/-MJtg3gLMC14zOm27HCB)

**Criação da Interface do Serviço**

Na Interface do Serviço definiremos os métodos a serem implementados no WebService.

Para criação do serviço deveremos implementar interface do mesmo e realizar as anotações para definição do Web Service e de seus métodos(@WebService, @WebMethod).

{% tabs %}
{% tab title="Interface Serviço" %}
{% code title="WebStoreService.java" %}

```java
package br.com.web.store.webservice;

import java.util.List;

import br.com.web.store.dto.InGameDTO;
import br.com.web.store.dto.OutGameDTO;

public interface WebStoreService {
	
	public List<OutGameDTO> listarJogos();
	
	public OutGameDTO buscarJogo(InGameDTO produtoDTO);
	
	public void desabilitarProduto(Integer id);

}

```

{% endcode %}
{% endtab %}

{% tab title="Implementação Interface" %}
{% code title="WebStoreServiceSoap.java" %}

```java
package br.com.web.store.webservice;

import java.util.List;

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

import org.springframework.scheduling.annotation.Async;

import br.com.web.store.dao.ProdutoRepo;
import br.com.web.store.dto.InGameDTO;
import br.com.web.store.dto.OutGameDTO;

@WebService
public class WebStoreServiceSoap implements WebStoreService {

	private ProdutoRepo repo = new ProdutoRepo();
	
	@WebMethod
	public List<OutGameDTO> listarJogos() {
		return repo.getListajogos();
	}

	@WebMethod
	public OutGameDTO buscarJogo(InGameDTO produtoDTO) {
		return repo.obterJogo(produtoDTO).get();
	}

	@Async
	@WebMethod
	public void desabilitarProduto(Integer id) {
		System.err.println("Produto Desabilitado com Sucesso!");
	}

}
```

{% endcode %}
{% endtab %}
{% endtabs %}

**Gerando o WebService**

Para a criação do web service usando o Framework Spring e o Apache CXF devemos criar um arquivo xml com a configuração do serviço, informando a classe definida com a anotação Web Service e o endpoint do serviço. Para isso criaremos o arquivo **webservice-definition-beans.xml** no diretório WEB-INF do WebContent do projeto.

```markup
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:jaxws="http://cxf.apache.org/jaxws"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">

 <bean id="webStoreServiceSoapBean" class="br.com.web.store.webservice.WebStoreServiceSoap" />

 <jaxws:endpoint id="webStoreServiceSoap" 
                 implementor="#webStoreServiceSoapBean" 
                 address="/service/WebStoreServiceSoap" 
                 publish="true"                
  publishedEndpointUrl="http://localhost:8080/app-service-store-ws-cxf/service/WebStoreServiceSoap" />

</beans>
```

Por fim devemos implementar o ContextListener do Spring e o Servlet do Apache CXF no arquivo web.xml

```markup
// trecho de código omitido
 <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>WEB-INF/webservice-definition-beans.xml</param-value>
  </context-param>
  <servlet>
    <servlet-name>CXFServlet</servlet-name>
    <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>CXFServlet</servlet-name>
    <url-pattern>/*</url-pattern>
    <url-pattern>/service</url-pattern>
  </servlet-mapping>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
```

O Endpoint do Serviço será publicado em: *<http://127.0.0.1:8080/app-service-store-ws-cxf/service/WebStoreServiceSoap?wsdl>*

![WSDL do Serviço](/files/-MJtjdKjMDmmCWkHazdO)

Fontes Disponíveis em: <https://github.com/pauloricmarinho/app-service-store-ws-cxf>


---

# 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/web-services-com-spring-e-apache-cxf.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.
