Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access web service using an ordinary java class?

**My Web service class**

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

/**
 * @author edward
 *
 */
@WebService
public class HelloWeb {

    @WebMethod
    public String sayGreeting(String name) {
        return "Greeting " + name + "....!";
    }

}

My Server java class

import javax.xml.ws.Endpoint;

public class Server {
    public static void main(String[] args) {
        Endpoint.publish("http://localhost:9090/HelloWeb", new HelloWeb());
        System.out.println("Hello Web service is ready");
    }
}

Server is running properly, and i am able to access the service using url that returns WSDL code.But i want to access the server using unique URL in java.I have the following client java code.

Client to access HelloWeb Service

import java.net.URL;

import javax.xml.namespace.QName;
import javax.xml.rpc.Service;
import javax.xml.rpc.ServiceFactory;

public class WebClient {
    String wsdl = "http://172.21.1.65:9090/HelloWeb?wsdl";
    String namespace = "http://helloweb.com";
    String serviceName = "HelloWebService";

    QName serviceQN = new QName(namespace, serviceName);

    {
        try{

        ServiceFactory serviceFactory = ServiceFactory.newInstance();
        Service service = serviceFactory.createService(new URL(wsdl), serviceQN);

        }catch (Exception e) {

        }
    }
}
like image 513
Edward Sagayaraj Avatar asked Nov 27 '12 12:11

Edward Sagayaraj


People also ask

How do I access a webservice in Java?

The java web service application can be accessed by other programming languages such as . Net and PHP. Java web service application perform communication through WSDL (Web Services Description Language). There are two ways to write java web service application code: SOAP and RESTful.

How do you invoke a web service?

If you want to invoke a web service, specify the name of the WEBSERVICE resource that defines the web service. The WEBSERVICE resource specifies the location of the web service description and the web service binding file that CICS uses when it communicates with the web service.


1 Answers

try this, note that I compiled and ran your server in "test" package, it's important. This is just a basic example to start with JAX-WS.

package test;

import java.net.URL;

import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;

public class WebClient {

    @WebService(name = "HelloWeb", targetNamespace = "http://test/")
    public interface HelloWeb {
        @WebMethod
        String sayGreeting(String name);
    }

    public static void main(String[] args) throws Exception {
        Service serv = Service.create(new URL(
                "http://localhost:9090/HelloWeb?wsdl"), 
                new QName("http://test/", "HelloWebService"));
        HelloWeb p = serv.getPort(HelloWeb.class);
        System.out.println(p.sayGreeting("John"));
    }
}
like image 86
Evgeniy Dorofeev Avatar answered Oct 06 '22 02:10

Evgeniy Dorofeev