Asdfasf

Friday, July 13, 2012

Simple JAX-WS WebService

Tomcat, WAR, AXIS ile ugrasmadan, JAX-WS ile standalone bir webservice ayaga kaldirmak bu kadar kolay:
Orjinali http://www.mkyong.com/webservices/jax-ws/jax-ws-hello-world-example/ da olan ornegi baz alip, sadece input parametrelerine Enum ve ArrayList ekleyip, annotation lar ile ornegi biraz daha zenginlestirmeye calistim.
JAX-WS annotation'larinin kullanimi icin :http://jax-ws.java.net/jax-ws-ea3/docs/annotations.html

Ilk adimimiz, acacagimiz servis'in interface'i

package com.mkyong.ws;

import javax.jws.WebMethod;
import javax.jws.WebParam;
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(@WebParam(name = "request") Request request);

}


Metodumuz Request objesi aliyor ki icerigi su sekilde:

package com.mkyong.ws;

import java.util.ArrayList;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;

public class Request {
    private String name;
    private CallType callType;
    private ArrayList<String> clients;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public CallType getCallType() {
        return callType;
    }

    public void setCallType(CallType callType) {
        this.callType = callType;
    }

    @XmlElementWrapper(name = "clients")
    @XmlElement(name = "client")
    public ArrayList<String> getClients() {
        return clients;
    }

    public void setClients(ArrayList<String> clients) {
        this.clients = clients;
    }

}


package com.mkyong.ws;

public enum CallType {
    TEST, PROD;
}


WebService interface'imizi implemente ediyoruz:

package com.mkyong.ws;

import javax.jws.WebService;

//Service Implementation
@WebService(endpointInterface = "com.mkyong.ws.HelloWorld")
public class HelloWorldImpl implements HelloWorld {

    @Override
    public String getHelloWorldAsString(Request request) {
        System.out.println(request.getClients().size());
        return "Hello World JAX-WS " + request.getName() + ", "
                + request.getCallType() + ", " + request.getClients().get(0);
    }
}

Ve en can alici nokta: Kodladigimiz buservisi tek satirda publish ediyoruz:

package com.mkyong.endpoint;

import javax.xml.ws.Endpoint;

import com.mkyong.ws.HelloWorldImpl;

//Endpoint publisher
public class HelloWorldPublisher {

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

}

WSDL'den wsimport ile client code uretip kullanmanin detaylari mkyong da mevcut, sadece HelloWorld interface'i elimizde mevcut ise client kod'u su sekilde olusturup kullanmamiz mumkun:


package com.mkyong.client;

import java.net.URL;
import java.util.ArrayList;

import javax.xml.namespace.QName;
import javax.xml.ws.Service;

import com.mkyong.ws.CallType;
import com.mkyong.ws.HelloWorld;
import com.mkyong.ws.Request;

public class HelloWorldClient {

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

        URL url = new URL("http://localhost:9999/ws/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.mkyong.com/",
                "HelloWorldImplService");

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

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

        ArrayList<String> clients = new ArrayList<String>();
        clients.add("c1");

        Request request = new Request();
        request.setName("ferhat");
        request.setCallType(CallType.TEST);
        request.setClients(clients);

        System.out.println(hello.getHelloWorldAsString(request));

    }
}

No comments: