**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 + "....!";
}
}
我的服务器java类
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");
}
}
服务器正常运行,我可以使用返回WSDL代码的url访问服务。但是我想在java中使用唯一的URL访问服务器。我有以下客户端java代码。
访问HelloWeb服务的客户端
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) {
}
}
}
答案 0 :(得分:1)
试试这个,请注意我在“test”包中编译和运行你的服务器,这很重要。这只是从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"));
}
}