JAVA – Como criar um WebService JAX-WS com SPRING 3

12 12 2013

Problema

Como criar um WebService JAX-WS junto com  Spring 3,  injetando os serviços manipulados pelo Spring.

Solução

A solução demostrada aqui é pontual com dicas nos principais arquivos, para mais detalhes coloquei alguns links de referencias de outros tutoriais mais completos no final do post.

  1. Configuração do pom.xml
  2. Configuração do  web.xml
  3. Criação dos Beans e Endpoint
    1. SayHelloServiceImp.java
    2. SayHelloService.java
    3. ServiceWs.java
  4. Configuração do applicationContext.xml (contexto Spring)
  5. Testes

1) pom.xml

<?xmlversion="1.0"encoding="UTF-8"?>
<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.0http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

  <groupId>com.wordpress.ldiasrs.servicews</groupId>
  <artifactId>ServicoWS</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>ServiceWS Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>${spring.version}</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>${spring.version}</version>
		</dependency>

		<dependency>
			<groupId>org.jvnet.jax-ws-commons.spring</groupId>
			<artifactId>jaxws-spring</artifactId>
			<version>1.9</version>
		</dependency>

	</dependencies>

	<properties>
		<spring.version>3.2.0.RELEASE</spring.version>
	</properties>
  <build>
       <finalName>ServicoWS</finalName>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-release-plugin</artifactId>
				<version>2.1</version>
			</plugin>
			<plugin>
				<artifactId>maven-compiler-plugin</artifactId>
				<configuration>
					<source>1.6</source>
					<target>1.6</target>
					<encoding>UTF-8</encoding>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-eclipse-plugin</artifactId>
				<version>2.8</version>
				<configuration>
					<downloadSources>true</downloadSources>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

2) web.xml

<web-app id="WebApp_ID" version="2.4" 
	xmlns="http://java.sun.com/xml/ns/j2ee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
        http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

	<display-name>Spring + JAX-WS</display-name>

    <!-- Register Spring Listener -->
  	<listener>
    	<listener-class>
    		org.springframework.web.context.ContextLoaderListener
    	</listener-class>
  	</listener>

  	<servlet>  
   <servlet-name>ServicoWs</servlet-name>  
   <servlet-class>  
     com.sun.xml.ws.transport.http.servlet.WSSpringServlet
   </servlet-class>  
</servlet>  

<servlet-mapping>  
   <servlet-name>ServiceWs</servlet-name>  
   <url-pattern>/sws</url-pattern>  
</servlet-mapping>  

</web-app>

3.1. Criar o SayHelloServiceImp.java

import org.springframework.stereotype.Service;

@Service
public class SayHelloServiceImp implements SayHelloService {
	@Override
	public String sayHello() {
		return "funcionou a paradinha :D";
	}
}

3.2. Criar o SayHelloService.java

public interface SayHelloService {

	String sayHello();

}

3.2. Criar o ServiceWs.java

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.support.SpringBeanAutowiringSupport;

@WebService
@Component(value = "ServiceWs")
public class ServiceWs extends SpringBeanAutowiringSupport {

    @Autowired
	private SayHelloService sayHelloService;

    @WebMethod
    public String sayHelloService() {
    	return sayHelloService.sayHello();
    }
}

4. applicationContext.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:ws="http://jax-ws.dev.java.net/spring/core"
    xmlns:wss="http://jax-ws.dev.java.net/spring/servlet"
    xsi:schemaLocation="
    http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
    http://jax-ws.dev.java.net/spring/core
    http://jax-ws.dev.java.net/spring/core.xsd
    http://jax-ws.dev.java.net/spring/servlet
    http://jax-ws.dev.java.net/spring/servlet.xsd">

  <context:component-scan base-package="com.wordpress.ldiasrs.servicews" />
  
  <wss:binding url="/sws">
    <wss:service>
      <ws:service bean="#ServiceWs" />
    </wss:service>
  </wss:binding>
</beans>

5. Testar

  1. Acessar a sua URL do projeto e verificar se o serviço subiu corretante
    1. ex: http://localhost:8080/ServiceWS/sws?wsdl
  2. Utilizar uma ferramenta de teste de Webservice para garantir que o SayHelloImp.java esta sendo injetado com sucesso e funcionando
    1. Eu utilizo o Web Service Explorer do Eclipse

 

Referencias mais completas: REF1 REF2 REF3 REF4

Anúncios

Ações

Information

Deixe um comentário

Preencha os seus dados abaixo ou clique em um ícone para log in:

Logotipo do WordPress.com

Você está comentando utilizando sua conta WordPress.com. Sair / Alterar )

Imagem do Twitter

Você está comentando utilizando sua conta Twitter. Sair / Alterar )

Foto do Facebook

Você está comentando utilizando sua conta Facebook. Sair / Alterar )

Foto do Google+

Você está comentando utilizando sua conta Google+. Sair / Alterar )

Conectando a %s




%d blogueiros gostam disto: