> For the complete documentation index, see [llms.txt](https://pauloricmarinho.gitbook.io/desenvolvimento/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://pauloricmarinho.gitbook.io/desenvolvimento/webservices/web-services-com-spring-e-apache-cxf.md).

# Web Services com Spring e Apache CXF

Criação de Web Services com Spring Framework 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:

![](https://750641972-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MHGW_meLjEK-3soe8kP%2F-MJrYo7joZUs8CyVI0Tc%2F-MJtg3gLMC14zOm27HCB%2Fimage.png?alt=media\&token=4ac9649e-d7c6-4883-bf15-79727752a56c)

**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](https://750641972-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MHGW_meLjEK-3soe8kP%2F-MJthVyx9Ct_V1wJbZMJ%2F-MJtjdKjMDmmCWkHazdO%2Fimage.png?alt=media\&token=5f38da30-4f2a-4bfd-b41e-70a12e5e423c)

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