一、开发服务端
(一)定义一个服务接口及实现类,并将实现类注解为@WebService,标注了该注解的类的方法就是webservice方法
(二)通过Endpoint的publish方法,将上一步定义的服务发布
(三)运行main方法
(四)在浏览器输入http://localhost:6868/userServiceImpl?wsdl ,查看webservice的wsdl的信息
package com.yu.service;
public interface UserService {
public String sayHi(String name);
public String work(String work);
}
package com.yu.service.impl;
import javax.jws.WebService;
import com.yu.service.UserService;
@WebService
public class UserServiceImpl implements UserService {
@Override
public String sayHi(String name) {
return "Hi webservice" + name;
}
@Override
public String work(String work) {
return "He is working" + work;
}
}
package com.yu.main;
import javax.xml.ws.Endpoint;
import com.yu.service.impl.UserServiceImpl;
public class WebService {
public static void main(String[] args) {
Endpoint.publish("http://localhost:6868/", new UserServiceImpl());
System.out.println("webservice 发布成功");
}
}
访问地址得到如下结果
<!--
Published by JAX-WS RI (http://jax-ws.java.net). RI's version is JAX-WS RI 2.2.9-b130926.1035 svn-revision#5f6196f2b90e9460065a4c2f4e30e065b245e51e.
-->
<!--
Generated by JAX-WS RI (http://jax-ws.java.net). RI's version is JAX-WS RI 2.2.9-b130926.1035 svn-revision#5f6196f2b90e9460065a4c2f4e30e065b245e51e.
-->
<definitions xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsp="http://www.w3.org/ns/ws-policy" xmlns:wsp1_2="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://impl.service.yu.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://impl.service.yu.com/" name="UserServiceImplService">
<types>
<xsd:schema>
<xsd:import namespace="http://impl.service.yu.com/" schemaLocation="http://localhost:6868/?xsd=1"/>
</xsd:schema>
</types>
<message name="sayHi">
<part name="parameters" element="tns:sayHi"/>
</message>
<message name="sayHiResponse">
<part name="parameters" element="tns:sayHiResponse"/>
</message>
<message name="work">
<part name="parameters" element="tns:work"/>
</message>
<message name="workResponse">
<part name="parameters" element="tns:workResponse"/>
</message>
<portType name="UserServiceImpl">
<operation name="sayHi">
<input wsam:Action="http://impl.service.yu.com/UserServiceImpl/sayHiRequest" message="tns:sayHi"/>
<output wsam:Action="http://impl.service.yu.com/UserServiceImpl/sayHiResponse" message="tns:sayHiResponse"/>
</operation>
<operation name="work">
<input wsam:Action="http://impl.service.yu.com/UserServiceImpl/workRequest" message="tns:work"/>
<output wsam:Action="http://impl.service.yu.com/UserServiceImpl/workResponse" message="tns:workResponse"/>
</operation>
</portType>
<binding name="UserServiceImplPortBinding" type="tns:UserServiceImpl">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
<operation name="sayHi">
<soap:operation soapAction=""/>
<input>
<soap:body use="literal"/>
</input>
<output>
<soap:body use="literal"/>
</output>
</operation>
<operation name="work">
<soap:operation soapAction=""/>
<input>
<soap:body use="literal"/>
</input>
<output>
<soap:body use="literal"/>
</output>
</operation>
</binding>
<service name="UserServiceImplService">
<port name="UserServiceImplPort" binding="tns:UserServiceImplPortBinding">
<soap:address location="http://localhost:6868/"/>
</port>
</service>
</definitions>
二、开发客户端
(一)根据服务端的wsdl文件生成客户端代码,将生成的代码拷贝的客户端项目下
在命令行输入命令 wsimport -encoding[编码] -keep[发布的服务地址?wsdl]
wsimport -encoding utf8 -p com.yu.webservice.stub -keep http://localhost:6868/userServiceImpl?wsdl
(二)使用生成的代码调用服务端提供的服务
package com.yu.webservice;
import com.yu.webservice.stub.UserServiceImpl;
import com.yu.webservice.stub.UserServiceImplService;
public class Webservice {
public static void main(String[] args) {
//1、生成webservice 客户端 (去生成的类里面找看看哪个类继承了 Service )
UserServiceImplService userServiceImplService = new UserServiceImplService();
//2、获取远程服务接口对象(用上边刚创建的客户端 .get 找后边带有Port的方法
UserServiceImpl userServiceImpl =userServiceImplService.getUserServiceImplPort();
//3、直接调用远程服务接口的方法
String hi = userServiceImpl.sayHi("web service");
System.out.println(hi);
String work = userServiceImpl.work("java");
System.out.println(work);
}
}