Spring + XFire == Qualidade + Facilidade

O XFire é a proxima geração de frameworks SOAP com um modelo de mensagens leves usado para integrar mensagens SOAP via STAX. Ele tem uma integração muito boa com o Spring Framework. Vou mostrar um exemplo de como fazer isso.

primeiro passo é definir uma interface neste exemplo vou definir a interface hora.




package org.diegopaheco.springxfire.model;
import javax.jws.WebService;

@WebService
public interface Hora {
  public String getDateTime();
}

Como vocês devem ter percebido eu só usei a annotation @WebService no XFire.

Agora vamos criar um serviço que implementa essa interface.

package org.diegopaheco.springxfire.model;

import java.util.Date;

import javax.jws.WebService;

@WebService(serviceName = "HoraService", endpointInterface = "org.diegopaheco.springxfire.model.Hora")
public class HoraImpl implements Hora {

     @SuppressWarnings("deprecation")
     public String getDateTime() {          
          return new Date().toLocaleString();
     }

}

Aqui temos a implementação da interface Hora e uma outra annotation:
@WebService(serviceName = "HoraService", endpointInterface = "org.diegopaheco.springxfire.model.Hora") Que define o nome do Servilo(HoraService) poderia ser qualquer outro e qual a interface desse serviço, nesse caso: org.diegopaheco.springxfire.model.Hora.

Agora vamos aos arquivos de configurações.
Existe um arquivo "padrão" que é o xfire-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>

 <bean id="webAnnotations" 
     class="org.codehaus.xfire.annotations.jsr181.Jsr181WebAnnotations"
 />
 
 <bean id="handlerMapping" class="org.codehaus.xfire.spring.remoting.Jsr181HandlerMapping">
        <property name="typeMappingRegistry">
            <ref bean="xfire.typeMappingRegistry"/>
        </property>
        <property name="xfire">
            <ref bean="xfire"/>
        </property>
        <property name="webAnnotations">
            <ref bean="webAnnotations"/>
        </property>
 </bean>

 <bean 
      class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"
 > 
      <property name="urlMap"> 
           <map> 
               <entry key="/">
                   <ref bean="handlerMapping"/>
               </entry>
           </map>
      </property>
 </bean>

 <import resource="classpath:org/codehaus/xfire/spring/xfire.xml"/> 

</beans>


No web.xml declaramos um servlet do XFire + o listener padrão do Spring.

<?xml version="1.0" encoding="UTF-8"?>
<web-app 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">
     
     <context-param>
          <param-name>contextConfigLocation</param-name>
         <param-value>
                      /WEB-INF/applicationContext.xml,
                  /WEB-INF/xfire-servlet.xml
          </param-value>
        </context-param>
        
    <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
     </listener>

     <!--
     <servlet>
          <servlet-name>context</servlet-name>
          <servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
          <load-on-startup>1</load-on-startup>
     </servlet>
     -->

     <servlet>
          <servlet-name>XFireServlet</servlet-name>
          <servlet-class>org.codehaus.xfire.spring.XFireSpringServlet</servlet-class>
    </servlet>

     <servlet-mapping>
          <servlet-name>XFireServlet</servlet-name>
          <url-pattern>/servlet/XFireServlet/*</url-pattern>
     </servlet-mapping>

     <servlet-mapping>
          <servlet-name>XFireServlet</servlet-name>
          <url-pattern>/services/*</url-pattern>
     </servlet-mapping>

</web-app>


E Finalmente o applicationContext.xml do Spring, ondi definimos os beans do Spring.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
      <bean id="annotatedHora" 
            class="org.diegopaheco.springxfire.model.HoraImpl"
      />       
 
</beans>


Para podermos expor o service do spring com o XFire é necessário as annotations + um serviço implementado + a definição do serviço como um spring bean.

Agora um programa de teste(client)

package org.diegopaheco.springxfire.test;

import java.net.MalformedURLException;

import org.codehaus.xfire.annotations.AnnotationServiceFactory;
import org.codehaus.xfire.client.XFireProxyFactory;
import org.codehaus.xfire.service.Service;
import org.diegopaheco.springxfire.model.Hora;
import org.diegopaheco.springxfire.model.HoraImpl;

public class TestRunnerHora {

     public static void main(String[] args) {
          Service serviceModel = new AnnotationServiceFactory()
                    .create(HoraImpl.class);

          try {
               Hora service = (Hora) new XFireProxyFactory().create(
                         serviceModel,
                         "http://localhost:8080/Spring-Xfire/services/HoraService");
               
               System.out.println("DateTime from web-service: " + service.getDateTime());
               
               
          } catch (MalformedURLException e) {
               e.printStackTrace();
          }

     }
     
}


É isso ae pp soal

Popular posts from this blog

C Unit Testing with Check

Having fun with Zig Language

HMAC in Java