本文共 3021 字,大约阅读时间需要 10 分钟。
package com.hyan.service; import javax.jws.WebService; import javax.xml.ws.Endpoint; @WebService public class HelloService { /** * 供客户端调用的方法 * @param name 传入参数 * @return String 返回结果 */ public String getValue(String name){ return "我叫:"+name; } /** * @param args */ public static void main(String[] args) { Endpoint.publish("http://localhost:9001/Service/HelloService", new HelloService()); System.out.println("service 调用成功!"); } }进行编译
说明:编译失败的话,请将该项目引用的jdk设置为1.6.0_17版本或更高版本
测试结果
测试地址:http://localhost:9001/Service/HelloService?wsdl
客户端测试
package com.hyan.test;
import com.hyan.client.ServiceHello; import com.hyan.client.ServiceHelloService; public class ServiceTest { /** * @param args */ public static void main(String[] args) { HelloService hello = new ServiceHelloService().getServiceHelloPort(); String name = hello.getValue("Hyan"); System.out.println(name); } } package com.hyan.client; import java.net.MalformedURLException; import java.net.URL; import java.util.logging.Logger; import javax.xml.namespace.QName; import javax.xml.ws.Service; import javax.xml.ws.WebEndpoint; import javax.xml.ws.WebServiceClient; import javax.xml.ws.WebServiceFeature; /** * This class was generated by the JAX-WS RI. * JAX-WS RI 2.1.6 in JDK 6 * Generated source version: 2.1 * */ @WebServiceClient(name = "ServiceHelloService", targetNamespace = "http://service.hyan.com/", wsdlLocation = "http://localhost:9001/Service/ServiceHello?wsdl") public class ServiceHelloService extends Service { private final static URL SERVICEHELLOSERVICE_WSDL_LOCATION; private final static Logger logger = Logger.getLogger(com.hyan.client.ServiceHelloService.class.getName()); static { URL url = null; try { URL baseUrl; baseUrl = com.hyan.client.ServiceHelloService.class.getResource("."); url = new URL(baseUrl, "http://localhost:9001/Service/ServiceHello?wsdl"); } catch (MalformedURLException e) { logger.warning("Failed to create URL for the wsdl Location: 'http://localhost:9001/Service/ServiceHello?wsdl', retrying as a local file"); logger.warning(e.getMessage()); } SERVICEHELLOSERVICE_WSDL_LOCATION = url; } public ServiceHelloService(URL wsdlLocation, QName serviceName) { super(wsdlLocation, serviceName); } public ServiceHelloService() { super(SERVICEHELLOSERVICE_WSDL_LOCATION, new QName("http://service.hyan.com/", "ServiceHelloService")); } /** * * @return * returns ServiceHello */ @WebEndpoint(name = "ServiceHelloPort") public ServiceHello getServiceHelloPort() { return super.getPort(new QName("http://service.hyan.com/", "ServiceHelloPort"), ServiceHello.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 ServiceHello */ @WebEndpoint(name = "ServiceHelloPort") public ServiceHello getServiceHelloPort(WebServiceFeature... features) { return super.getPort(new QName("http://service.hyan.com/", "ServiceHelloPort"), ServiceHello.class, features); } }转载地址:http://xgcws.baihongyu.com/