Tuesday, February 14, 2012

Web Services using JAX-RPC, JAX-WS or JAX-RS

Quickstart WebServices Tutorial


JAX-RS 

Weblogic 12c version 12.1.2.1 now ships by default with JAX-RS 1.1 as a core library.  Previously in Weblogic 12.1.1.0 the following shared web applications were required - now we just use the specification library.

 Library jackson-core-asl(1.0,1.1.1) Active  Library
 Library jersey-bundle(1.1.1,1.1.5.1) Active  Library
 Library jettison(1.1,1.1) Active  Library
 Library jsr311-api(1.1.1,1.1.1) Active  Library

Now in eclipse just add the JAX-RS facet which will add the jersey servlet to your web.xml

  <servlet>
    <description>JAX-RS Tools Generated - Do not modify</description>
    <servlet-name>JAX-RS Servlet</servlet-name>
    <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>JAX-RS Servlet</servlet-name>
    <url-pattern>/jaxrs/*</url-pattern>
  </servlet-mapping>



JAX-RPC

Jax-RPC has fallen out of favor for most new web services in favor of JAX-RS 1.1 or REST.
A simple java SE example that is single threaded and runs off of Metro included in Java SE 1.6.
This example does not need a web container or a full application server to run - however it is non-scalable and thread-blocking simple example.


ServiceEndpoint.java
package org.dataparallel.example.jaxws;

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

/**
* definitions targetNamespace="http://jaxws.example.dataparallel.org/" name="ServiceEndpointImplService
* 
*/

@WebService
@SOAPBinding(style = Style.RPC)
public interface ServiceEndpoint {
    public static final String URL = http://127.0.0.1;
    public static final String PORT = "8980";
    public static final String SERVICE_NAME = "service";
    public static final String SERVICE_URI = URL + ":" + PORT + "/" + SERVICE_NAME;
    public static final String DOMAIN_NAME = "http://jaxws.example.dataparallel.org/";


    @WebMethod
    public String getImage();
}

ServiceEndpointImpl.java
package org.dataparallel.example.jaxws;

import javax.jws.WebService;

@WebService(endpointInterface="org.dataparallel.example.jaxws.ServiceEndpoint")
public class ServiceEndpointImpl implements ServiceEndpoint {
    @Override
    public String getImage() {
        System.out.println("_ServiceEndpointImpl.getImage()");
        return "aString";
    }
}

ServiceEndpointPublisher.java
package org.dataparallel.example.jaxws;

import javax.xml.ws.Endpoint;
public class ServiceEndpointPublisher {
    public void publish() {
        publish(ServiceEndpoint.SERVICE_URI, new ServiceEndpointImpl());
    }

    public void publish(String url, ServiceEndpoint service) {
        Endpoint.publish(url, service);
    }

    /**
     * http://127.0.0.1:8980/service?wsdl
     */
    public static void main(String[] args) {
        // single threaded testing service
        ServiceEndpointPublisher aPublisher = new ServiceEndpointPublisher();
        aPublisher.publish(); 
    }

ServiceClient.java
package org.dataparallel.example.jaxws;


import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;

public class ServiceClient {
    public void callService() {
        try {
            URL aWsdlURL = new URL(ServiceEndpoint.SERVICE_URI + "?wsdl");
            QName aQName = new QName(ServiceEndpoint.DOMAIN_NAME,ServiceEndpointImpl.class.getSimpleName() + "Service");
            Service aService = Service.create(aWsdlURL, aQName);
            ServiceEndpoint anEndpointInterface = aService.getPort(ServiceEndpoint.class);
            System.out.println("Service.getImage: " + anEndpointInterface.getImage());
        } catch (MalformedURLException mue) {
            mue.printStackTrace();
    }
}

    public static void main(String[] args) {
        ServiceClient aClient = new ServiceClient();
        aClient.callService();
    }
}



JAX-WS
Jax-WS has fallen out of favor for most new web services in favor of JAX-RS 1.1 or REST - it still has a place for specific SOAP clients or data types.

JAX-RS
- in queue - see
http://wiki.eclipse.org/EclipseLink/Development/2.4.0/JPA-RS/REST-API

No comments:

Total Pageviews

Followers