webservice发布方法

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_37279783/article/details/86242403

1.接口和实现类: 

接口:
import javax.jws.WebMethod;
import javax.jws.WebService;

@WebService
public interface HelloWorld {
    /**
    * exclude=true,此方法不对外暴露
    */
    @WebMethod(exclude=true)
    String sayHi(String text);
    Integer getSquare(Integer num);
}

实现类:
import javax.jws.WebService;

@WebService(endpointInterface = "ws.HelloWorld",serviceName = "HelloWorld")
public class HelloWorldImpl implements HelloWorld {
    @Override
    public String sayHi(String text) {
        System.out.println("sayHi called");
        return "Hello " + text;
    }
    @Override
    public Integer getSquare(Integer num){
        return num*num;
    }
}

2.服务的发布和客户端

方法1:
服务发布1:
import javax.xml.ws.Endpoint;

public class publish {
    public static void main(String[] args) {
        HelloWorldImpl implementor = new HelloWorldImpl();
        String address = "http://localhost:9000/helloWorld";
        Endpoint.publish(address, implementor);
    }
}

客户端1:
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import javax.xml.ws.soap.SOAPBinding;
import java.net.URL;

public class client {
    private static final QName SERVICE_NAME
            = new QName("http://ws/", "HelloWorld");
    private static final QName PORT_NAME
            = new QName("http://ws/", "HelloWorldPort");

    public static void main(String args[]) throws Exception {
        Service service = Service.create(SERVICE_NAME);
        //Service service = Service.create(new URL("http://localhost:9000/helloWorld?wsdl"),SERVICE_NAME);
        // Endpoint Address
        String endpointAddress = "http://localhost:9000/helloWorld";
        // If web service deployed on Tomcat (either standalone or embedded)
        // as described in sample README, endpoint should be changed to:
        // String endpointAddress = "http://localhost:8080/java_first_jaxws/services/hello_world";

        // Add a port to the Service
        service.addPort(PORT_NAME, SOAPBinding.SOAP11HTTP_BINDING, endpointAddress);

        HelloWorld hw = service.getPort(HelloWorld.class);
        System.out.println(hw.sayHi("my baby2"));

        System.out.println(hw.getSquare(123));

    }
}
方法2:
服务端2:
import org.apache.cxf.jaxws.JaxWsServerFactoryBean;
import ws.HelloWorld;
import ws.HelloWorldImpl;

public class publish {
    public static void main(String[] args) {
        HelloWorldImpl implementor = new HelloWorldImpl();
        JaxWsServerFactoryBean svrFactory = new JaxWsServerFactoryBean();
        svrFactory.setServiceClass(HelloWorld.class);
        svrFactory.setAddress("http://localhost:9000/helloWorld");
        svrFactory.setServiceBean(implementor);
        svrFactory.create();
    }
}

客户端2:
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import ws.HelloWorld;

public class client {
    public static void main(String args[]) throws Exception {
        JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
        factory.setServiceClass(HelloWorld.class);
        factory.setAddress("http://localhost:9000/helloWorld");
        HelloWorld client = (HelloWorld) factory.create();
        String reply = client.sayHi("HI");
        System.out.println("Server said: " + reply);
    }
}

猜你喜欢

转载自blog.csdn.net/qq_37279783/article/details/86242403