JAX-WS webservice服务与客户端

package com.example.webservice;

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

@WebService
public interface IGreetingService {
    @WebMethod
    String sendGreetings(String name);

    @WebMethod
    String getGreeting();
}
package com.example.webservice;

import javax.jws.WebService;
import javax.xml.ws.Endpoint;

@WebService
public class GreetingServiceImpl implements IGreetingService {
    @Override
    public String sendGreetings(String name) {
        return "Hello, " + name;
    }

    @Override
    public String getGreeting() {
        return "Hello, world!";
    }

    public static void main(String[] args) {
        String address = "http://localhost:8888/";
        Endpoint.publish(address, new GreetingServiceImpl());
    }
}

客户端

package com.example.webservice;

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

public class WebServiceClient {
    public static void main(String[] args) throws Exception{
        QName serviceName = new QName("http://webservice.example.com/", "GreetingServiceImplService");
        QName portName = new QName("http://webservice.example.com/", "GreetingServiceImplPort");
        String address = "http://localhost:8888/?wsdl";
        URL url = new URL(address);
        Service service = Service.create(url, serviceName);
        IGreetingService webservice = service.getPort(portName, IGreetingService.class);
        System.out.println(webservice.getGreeting());
        System.out.println(webservice.sendGreetings("zhangsan"));
    }
}


你可能感兴趣的:(JAX-WS,Webservice)