JAX – WS – 10 – wsimport

Merhaba arkadaslar
Bu bolumde wsimport tool’unu inceleyecegiz.
wsimport ile wsdl(web service definition language) dosyalarini parse edebiliriz, ilgili JAX-WS /SOAP service’ni kullanmak icin gerekli .java dosyalarini otomatik olarak olusturabiliriz.

You use the wsimport command to generate Java API for XML Web Services 
(JAX-WS) portable artifacts.

https://docs.oracle.com/javase/9/tools/wsimport.htm#JSWOR740

Oncelikle ilk ornegimizi tekrar kullanalim. Bu ornegimizde basit bir JAX-WS ornegi uygulamistik.
http://www.injavawetrust.com/jax-ws-02-simple-jax-ws-implementation/

Projeyi 8888 portunda publish etmistik. Tekrar ornegimizi calistiralim ve basarili sekilde wsdl yapisini gorelim.

http://localhost:8888/injavawetrust/hello.jaxws?wsdl

Simdi de command line’ da wsimport komutunu kullanalim.
Dosyalari ornek olarak Desktop/wsimport.example klasorunde olusturacagim.

wsimport -keep -verbose http://localhost:8888/injavawetrust/hello.jaxws?wsdl

Her sey yolundaysa .class ve .java dosyalarimiz basarili sekilde olusacaktir. (-keep) parametresini kullanmazsak sadece .class dosyalari olusur)
-verbose parametresi compiler’in mesajlarini gosterecektir.

-verbose
Displays compiler messages.
Output messages about what the compiler is doing

Diger parametreleri de kullanabiliriz;
-d parametresi ile dosyalarimizin olusacagi output folder’i verebiliriz.
-p parametresi ile dosyalarimizin ait oldugu package bilgisini verebiliriz.

-d directory
Specifies where to place generated output files.
-p name
Specifies a target package name to override the WSDL file and schema binding customizations, and the default algorithm defined in the specification.
wsimport -verbose -d mydir -p mypackage http://localhost:8888/injavawetrust/hello.jaxws?wsdl

mydir folder’i altinda , mypackage paket yapisinda ilgili dosyalarimiz olusacaktir.

Ilk olusturdugumuz dosyalari kullanabiliriz.

MessageWriter.java

package injavawetrust.ws.service;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.xml.ws.Action;


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


    /**
     * 
     * @param surname
     * @param name
     * @return
     *     returns java.lang.String
     */
    @WebMethod
    @WebResult(partName = "return")
    @Action(input = "http://service.ws.injavawetrust/MessageWriter/getWelcomeMessageRequest", output = "http://service.ws.injavawetrust/MessageWriter/getWelcomeMessageResponse")
    public String getWelcomeMessage(
        @WebParam(name = "name", partName = "name")
        String name,
        @WebParam(name = "surname", partName = "surname")
        String surname);

}

MessageWriterImplService.java

package injavawetrust.ws.service;

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 = "MessageWriterImplService", targetNamespace = "http://service.ws.injavawetrust/", wsdlLocation = "http://localhost:8888/injavawetrust/hello.jaxws?wsdl")
public class MessageWriterImplService
    extends Service
{

    private final static URL MESSAGEWRITERIMPLSERVICE_WSDL_LOCATION;
    private final static WebServiceException MESSAGEWRITERIMPLSERVICE_EXCEPTION;
    private final static QName MESSAGEWRITERIMPLSERVICE_QNAME = new QName("http://service.ws.injavawetrust/", "MessageWriterImplService");

    static {
        URL url = null;
        WebServiceException e = null;
        try {
            url = new URL("http://localhost:8888/injavawetrust/hello.jaxws?wsdl");
        } catch (MalformedURLException ex) {
            e = new WebServiceException(ex);
        }
        MESSAGEWRITERIMPLSERVICE_WSDL_LOCATION = url;
        MESSAGEWRITERIMPLSERVICE_EXCEPTION = e;
    }

    public MessageWriterImplService() {
        super(__getWsdlLocation(), MESSAGEWRITERIMPLSERVICE_QNAME);
    }

    public MessageWriterImplService(WebServiceFeature... features) {
        super(__getWsdlLocation(), MESSAGEWRITERIMPLSERVICE_QNAME, features);
    }

    public MessageWriterImplService(URL wsdlLocation) {
        super(wsdlLocation, MESSAGEWRITERIMPLSERVICE_QNAME);
    }

    public MessageWriterImplService(URL wsdlLocation, WebServiceFeature... features) {
        super(wsdlLocation, MESSAGEWRITERIMPLSERVICE_QNAME, features);
    }

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

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

    /**
     * 
     * @return
     *     returns MessageWriter
     */
    @WebEndpoint(name = "MessageWriterImplPort")
    public MessageWriter getMessageWriterImplPort() {
        return super.getPort(new QName("http://service.ws.injavawetrust/", "MessageWriterImplPort"), MessageWriter.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 MessageWriter
     */
    @WebEndpoint(name = "MessageWriterImplPort")
    public MessageWriter getMessageWriterImplPort(WebServiceFeature... features) {
        return super.getPort(new QName("http://service.ws.injavawetrust/", "MessageWriterImplPort"), MessageWriter.class, features);
    }

    private static URL __getWsdlLocation() {
        if (MESSAGEWRITERIMPLSERVICE_EXCEPTION!= null) {
            throw MESSAGEWRITERIMPLSERVICE_EXCEPTION;
        }
        return MESSAGEWRITERIMPLSERVICE_WSDL_LOCATION;
    }

}

Test Client

Simdi de Client Test sinifimizi yazalim.
MessageWriterImplService objesi uzerinden getMessageWriterImplPort metodunu kullabiliriz.
MessageWriter objesi uzerinden getWelcomeMessage’ni test edebiliriz.

package client;

import injavawetrust.ws.service.MessageWriter;
import injavawetrust.ws.service.MessageWriterImplService;

public class ClientTest {

	public static void main(String[] args) {
		MessageWriterImplService service = new MessageWriterImplService();
		MessageWriter messageWriter = service.getMessageWriterImplPort();
		String message = messageWriter.getWelcomeMessage("levent", "erguder");
		System.out.println(message);
	}
}

// 1) run , injavawetrust.hello.jaxws project WebServicePublisher.java

Ornegi calistirdigimizda;

Welcome , levent erguder. This message is sent by MessageWriterImpl#getWelcomeMessage.

Tabiki diger projemizdeki WebServicePublisher sinfi hali hazirda calisiyor olmali. Aksi durumda connection refused hatasi aliriz.

Exception in thread "main" javax.xml.ws.WebServiceException: Failed to access the WSDL at: http://localhost:8888/injavawetrust/hello.jaxws?wsdl. It failed with: 
    Connection refused (Connection refused).
    at com.sun.xml.internal.ws.wsdl.parser.RuntimeWSDLParser.tryWithMex(RuntimeWSDLParser.java:250)
    at com.sun.xml.internal.ws.wsdl.parser.RuntimeWSDLParser.parse(RuntimeWSDLParser.java:231)
    at com.sun.xml.internal.ws.wsdl.parser.RuntimeWSDLParser.parse(RuntimeWSDLParser.java:194)
    at com.sun.xml.internal.ws.wsdl.parser.RuntimeWSDLParser.parse(RuntimeWSDLParser.java:163)
    at com.sun.xml.internal.ws.client.WSServiceDelegate.parseWSDL(WSServiceDelegate.java:348)
    at com.sun.xml.internal.ws.client.WSServiceDelegate.(WSServiceDelegate.java:306)
    at com.sun.xml.internal.ws.client.WSServiceDelegate.(WSServiceDelegate.java:215)
    at com.sun.xml.internal.ws.client.WSServiceDelegate.(WSServiceDelegate.java:196)
    at com.sun.xml.internal.ws.client.WSServiceDelegate.(WSServiceDelegate.java:192)
    at com.sun.xml.internal.ws.spi.ProviderImpl.createServiceDelegate(ProviderImpl.java:104)
    at javax.xml.ws.Service.(Service.java:77)
    at injavawetrust.ws.service.MessageWriterImplService.(MessageWriterImplService.java:42)
    at client.ClientTest.main(ClientTest.java:9)
Caused by: java.net.ConnectException: Connection refused (Connection refused)
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:589)
    at java.net.Socket.connect(Socket.java:538)
    at sun.net.NetworkClient.doConnect(NetworkClient.java:180)
    at sun.net.www.http.HttpClient.openServer(HttpClient.java:432)
    at sun.net.www.http.HttpClient.openServer(HttpClient.java:527)
    at sun.net.www.http.HttpClient.(HttpClient.java:211)
    at sun.net.www.http.HttpClient.New(HttpClient.java:308)
    at sun.net.www.http.HttpClient.New(HttpClient.java:326)
    at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:1202)
    at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1138)
    at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:1032)
    at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:966)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1546)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1474)
    at java.net.URL.openStream(URL.java:1045)
    at com.sun.xml.internal.ws.wsdl.parser.RuntimeWSDLParser.createReader(RuntimeWSDLParser.java:984)
    at com.sun.xml.internal.ws.wsdl.parser.RuntimeWSDLParser.resolveWSDL(RuntimeWSDLParser.java:385)
    at com.sun.xml.internal.ws.wsdl.parser.RuntimeWSDLParser.parse(RuntimeWSDLParser.java:216)
    ... 11 more

Github kaynak kodlar / source folder
injavawetrust-jaxws-tutorial

Yazimi burada sonlandiriyorum.
Herkese bol Javali gunler dilerim.
Be an oracle man , import java.*;

Print Friendly, PDF & Email

Leave a Reply

Your email address will not be published.