jueves, 12 de octubre de 2017

Crear webservice con JAX-WS (RPC)

1. Crear la siguiente clase HelloWorld:

package com.luis.ws;

import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;

//Service Endpoint Interface
@WebService


@SOAPBinding(style = Style.RPC)
public interface HelloWorld{

@WebMethod 
String getHelloWorldAsString(String name);

}

2. Crear esta otra clase HelloWorldImpl :

package com.luis.ws;

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

//Service Implementation Bean

@WebService(endpointInterface = "com.luis.ws.HelloWorld")
public class HelloWorldImpl implements com.luis.ws.HelloWorld {


public String getHelloWorldAsString(String name) {
return "Hello World JAX-WS " + name;
}
}

3. Crear la clase HelloWorldPublisher :

package com.luis.ws;

import javax.xml.ws.Endpoint;
import com.luis.ws.HelloWorldImpl;

//Endpoint publisher
public class HelloWorldPublisher {

public static void main(String[] args) {
Endpoint.publish("http://localhost:8081/hello", new HelloWorldImpl());
}

}

4. Crear el sigueinte archivo sun-jaxws.xml en WEB-INF

 <?xml version="1.0" encoding="UTF-8"?>
<endpoints
  xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime"
  version="2.0">
  <endpoint
      name="HelloWorld"
      implementation="com.luis.ws.HelloWorldImpl"
      url-pattern="/hello"/>
</endpoints>

5. Crear archivo web.xml en WEB-INF.

<web-app id="WebApp_ID" version="2.5"
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/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<listener>
<listener-class>
com.sun.xml.ws.transport.http.servlet.WSServletContextListener
</listener-class>
</listener>
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>120</session-timeout>
</session-config>
</web-app>

6. Crear el siguiente archivo pom.xml
<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.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>1</groupId>
<artifactId>Soap</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>Soap Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>

<!-- https://mvnrepository.com/artifact/javax.xml.ws/jaxws-api -->
<dependency>
<groupId>javax.xml.ws</groupId>
<artifactId>jaxws-api</artifactId>
<version>2.2.6</version>
</dependency>


<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>

<!-- https://mvnrepository.com/artifact/com.sun.xml.ws/jaxws-rt -->
<dependency>
<groupId>com.sun.xml.ws</groupId>
<artifactId>jaxws-rt</artifactId>
<version>2.1.3</version>
</dependency>


<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>Soap</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

6. Hay dos formas para correr el webservice, una es crear el archivo WAR y hacer deployment en tomcat o correr el programa HelloWorldPublisher. Si se corre la clase HelloWorldPublisher se usa la siguiente url
http://localhost:8081/hello?wsdl

7. Para crear los stubs del cliente se utiliza la herramienta wsimport

wsimport -keep http://localhost:9999/ws/hello?wsdl
7. Se generaran los siguientes stubs:  HelloWorldImplService


package com.luis.ws;

import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import javax.xml.ws.WebEndpoint;
import javax.xml.ws.WebServiceClient;
import javax.xml.ws.WebServiceException;
import javax.xml.ws.WebServiceFeature;


/**
 * This class was generated by the JAX-WS RI.
 * JAX-WS RI 2.2.9-b130926.1035
 * Generated source version: 2.2
 * 
 */
@WebServiceClient(name = "HelloWorldImplService", targetNamespace = "http://ws.luis.com/", wsdlLocation = "http://localhost:8081/hello?wsdl")
public class HelloWorldImplService
    extends Service
{

    private final static URL HELLOWORLDIMPLSERVICE_WSDL_LOCATION;
    private final static WebServiceException HELLOWORLDIMPLSERVICE_EXCEPTION;
    private final static QName HELLOWORLDIMPLSERVICE_QNAME = new QName("http://ws.luis.com/", "HelloWorldImplService");

    static {
        URL url = null;
        WebServiceException e = null;
        try {
            url = new URL("http://localhost:8081/hello?wsdl");
        } catch (MalformedURLException ex) {
            e = new WebServiceException(ex);
        }
        HELLOWORLDIMPLSERVICE_WSDL_LOCATION = url;
        HELLOWORLDIMPLSERVICE_EXCEPTION = e;
    }

    public HelloWorldImplService() {
        super(__getWsdlLocation(), HELLOWORLDIMPLSERVICE_QNAME);
    }

    public HelloWorldImplService(WebServiceFeature... features) {
        super(__getWsdlLocation(), HELLOWORLDIMPLSERVICE_QNAME, features);
    }

    public HelloWorldImplService(URL wsdlLocation) {
        super(wsdlLocation, HELLOWORLDIMPLSERVICE_QNAME);
    }

    public HelloWorldImplService(URL wsdlLocation, WebServiceFeature... features) {
        super(wsdlLocation, HELLOWORLDIMPLSERVICE_QNAME, features);
    }

    public HelloWorldImplService(URL wsdlLocation, QName serviceName) {
        super(wsdlLocation, serviceName);
    }

    public HelloWorldImplService(URL wsdlLocation, QName serviceName, WebServiceFeature... features) {
        super(wsdlLocation, serviceName, features);
    }

    /**
     * 
     * @return
     *     returns HelloWorld
     */
    @WebEndpoint(name = "HelloWorldImplPort")
    public HelloWorld getHelloWorldImplPort() {
        return super.getPort(new QName("http://ws.luis.com/", "HelloWorldImplPort"), HelloWorld.class);
    }

    /**
     * 
     * @param features
     *     A list of {@link javax.xml.ws.WebServiceFeature} to configure on the proxy.  Supported features not in the <code>features</code> parameter will have their default values.
     * @return
     *     returns HelloWorld
     */
    @WebEndpoint(name = "HelloWorldImplPort")
    public HelloWorld getHelloWorldImplPort(WebServiceFeature... features) {
        return super.getPort(new QName("http://ws.luis.com/", "HelloWorldImplPort"), HelloWorld.class, features);
    }

    private static URL __getWsdlLocation() {
        if (HELLOWORLDIMPLSERVICE_EXCEPTION!= null) {
            throw HELLOWORLDIMPLSERVICE_EXCEPTION;
        }
        return HELLOWORLDIMPLSERVICE_WSDL_LOCATION;
    }

}

Se generaran los siguientes stubs:  HelloWorldInterface


package com.luis.ws;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;


/**
 * This class was generated by the JAX-WS RI.
 * JAX-WS RI 2.2.9-b130926.1035
 * Generated source version: 2.2
 * 
 */
@WebService(name = "HelloWorld", targetNamespace = "http://ws.luis.com/")
@SOAPBinding(style = SOAPBinding.Style.RPC)
public interface HelloWorldInterface {


    /**
     * 
     * @param arg0
     * @return
     *     returns java.lang.String
     */
    @WebMethod
    @WebResult(partName = "return")
    public String getHelloWorldAsString(
        @WebParam(name = "arg0", partName = "arg0")
        String arg0);

}

Nosotros crearemos nuestro propio cliente en base a estos stubs:
package com.luis.ws;

import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import com.luis.ws.HelloWorld;

public class HelloWorldClient {

public static void main(String[] args) throws Exception {

URL url = new URL("http://localhost:8081/hello?wsdl");

// 1st argument service URI, refer to wsdl document above
// 2nd argument is service name, refer to wsdl document above
QName qname = new QName("http://ws.luis.com/", "HelloWorldImplService");

Service service = Service.create(url, qname);

HelloWorld hello = service.getPort(HelloWorld.class);

System.out.println(hello.getHelloWorldAsString("mario"));

}
}

Otra forma de probar el web service es haciendo un POST con el request XML para SOAP.

<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <ns1:getHelloWorldAsString xmlns:ns1="http://ws.luis.com/">
      <arg0>Enfermera</arg0>
    </ns1:getHelloWorldAsString>
  </soap:Body>

</soap:Envelope>

Devolvera algo asi:
<?xml version="1.0" ?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
    <S:Body>
        <ns2:getHelloWorldAsStringResponse xmlns:ns2="http://ws.luis.com/">
            <return>Hello World JAX-WS Enfermera</return>
        </ns2:getHelloWorldAsStringResponse>
    </S:Body>

</S:Envelope>





No hay comentarios:

Publicar un comentario