个人这几天一直在研修WebService,记录一下这几天的总结(本文服务端客户端并不是相对应的一个服务)
服务端
服务提供端可以写接口,也可以不写接口直接写实现类
接口
@WebService
public interface HaoWsService {
@WebMethod(action = "http://service.webservice.hao.com/sendhao")
public String sendhao(
@WebParam(name = "suser", targetNamespace = "http://service.webservice.hao.com/") String suser,
@WebParam(name = "spwd", targetNamespace = "http://service.webservice.hao.com/") String spwd,
) throws Exception;
action = “http://service.webservice.hao.com/sendhao” > 定义方法在此服务的详细地址,可省略但最好还是写上
@WebParam:
name = “suser”, 指定web服务此方法的的参数名字(重要)
targetNamespace = “http://service.webservice.hao.com/”
同样的作用
实现类
@WebService(targetNamespace = "http://service.webservice.hao.com/", endpointInterface = "com.hao.webservice.service.haoWsService")
public class HaoWsServiceImpl implements HaoWsService {
@Override
public String HaoWsService (String sUser, String sPwd) throws Exception {
System.out.println(sUser+"---"+sPwd);
return sUser+"---"+sPwd;
}
targetNamespace :(重要)targetNamespace是一个xml的schema中的概念 比如我们定义了这么个schema:
<xs:schema xmlns:xs= "http://www.w3.org/2001/XMLSchema "
targetNamespace= "http://a.name/space "> <xs:element name= "address " type= "xs:string " /> </xs:schema>
那么它表示的意思是address这个元素是属于 "http://a.name/space "命名空间的。你想想看,如果你不指定targetNamespace,那么address是属于什么命名空间是不知道的,它肯定不是属于“http://www.w3.org/2001/XMLSchema”命名空间。指定了这个以后,就能让我们定义的schema中的元素都有自己的命名空间。这个命名空间都是自己定义的。
我想targetNamespace= "http://a.name/space "就是为你自己定义的元素定义了一个包,也就是package的概念,你的这个元素是这个package(命名空间)里的,在别的XML文件里面你可以用<xs:schema xmlns:s= “http://a.name/space” />来引用你前面定义的元素,这里就相当于import的概念了。
默认是目录的倒叙
在wsdl中的位置:
endpointInterface :定义服务抽象 web service 协定的服务端点接口的完整名称。
此注释允许开发人员将接口协定与实现分离。如果此注释存在,则使用服务端点接口来确定抽象 wsdl 协定(porttype 和绑定)。服务端点接口可以包含用来定制 java 到 wsdl 的映射关系的 jsr-181 注释。
服务实现 bean 可以实现服务端点接口,但并不要求这样做。
如果此成员值不存在,则 web service 协定是根据服务实现 bean 上的注释生成的。如果目标环境需要一个服务端点接口,则将生成一个由实现定义的包,该包具有由实现定义的名称。
不允许在端点接口上使用此成员值。 默认值:""
暴漏服务
public class HelloWorld {
public static void main(String[] argv) {
Object HaoWsServiceImpl = new HaoWsServiceImpl ();
String address = "http://localhost:9000/HelloWorld";
Endpoint.publish(address, HaoWsServiceImpl );
}
}
客户端(三种调用方式)
第一种(生成服务端的代码远程调用服务端发布在云上的服务)
生成本地代码有两种方式:
方式一:
1.通过wsdl地址之后右键生产xml文件,修改后缀名为wsdl文件;或者服务端之间给wsdl文件
2.生成本地代码,用命令 wsimport -s . ***.wsdl
生成本地代码
方式二:
在这个文件夹下执行命令 wsdl2java ***.wsdl
,没有的话可以去下http://cxf.apache.org/download.html
然后在本地运行就可以了
本地运行一般都是找有get***Soap的类,然后新建这个列,通过get***Soap获得soap,
用soap调用具体的方法。通过调用get***Soap可以获取多个soap实体类,多次调用服务
public class ClientTest {
public static void main(String[] args) {
WeatherWebService service = new WeatherWebService();
WeatherWebServiceSoap soap = service.getWeatherWebServiceSoap();
ArrayOfString weatherbyCityName = soap.getWeatherbyCityName("成都");
System.out.println(weatherbyCityName.getString());
}
}
第二种(用org.apache.axis方式)
package maven.webtest;
import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;
public class ClientTest {
public static void main(String[] args) {
String endpoint= "http://localhost:9000/HelloWorld?wsdl";
String result = "";
Service service = new Service();//org.apache.axis.client.Service;
Call call;//org.apache.axis.client.Call;
try {
call=(Call) service.createCall();
call.setTargetEndpointAddress(endpoint);
QName qName = new QName("http://example/","sayHelloWorldFrom");//非常重要,下面解释
call.setOperationName(qName);
call.addParameter("from", XMLType.XSD_STRING, ParameterMode.IN);//这里对象服务端接口方法参数的名字(重要,要不然参数传不过去,传过去为null)
call.setReturnType(XMLType.XSD_STRING);//返回参数的类型
result = (String)call.invoke(new Object[] {"啦啦啦啦啦"});//调用具体的服务,并进行传参
System.out.println("result:"+result);//返回值
} catch (Exception e) {
e.printStackTrace();
}
}
}
QName qName = new QName(“http://example/”,“sayHelloWorldFrom”)
第一个参数就是服务端对应的 targetNamespace(服务的命名空间)
第二个参数就是具体服务接口方法的名字
需要的jar包
<dependencies>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.axis</groupId>
<artifactId>axis</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>commons-discovery</groupId>
<artifactId>commons-discovery</artifactId>
<version>0.2</version>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.axis</groupId>
<artifactId>axis-jaxrpc</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>org.apache.axis</groupId>
<artifactId>axis-saaj</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>wsdl4j</groupId>
<artifactId>wsdl4j</artifactId>
<version>1.4</version>
</dependency>
</dependencies>
第三种
public static String testOAWebService(){
String result = "";
try {
Client c = new Client(new URL("http://10.3.100.100/services/WorkflowService?wsdl"));
Object[] params = new Object[5];
params[0]=null;
params[1]=new Integer(1497276);//工作流请求ID
params[2]=new Integer(26626);//空节点操作人ID
params[3]="submit";//参数为reject时退回流程,为submit时提交流程
params[4]="手动测试OA接口";//
Object[] results = c.invoke("submitWorkflowRequest", params);
result = results[0].toString();
} catch (MalformedURLException e) {
System.out.println(e);
e.printStackTrace();
} catch (Exception e) {
System.out.println(e);
e.printStackTrace();
}
System.out.println("result:"+result);
return result;
}