基于契约优先来编写webservice

[b]
开发流程:
1.先写schema或者wsdl文件
2.根据这个文件生成客户端代码
3.编写实现类(在实现类上指定wsdlLocation)
4.发布服务
[/b]

[size=medium]
src下创建META-INF目录,再该目录下创建wsdl文件夹,使用向导创建一个wsdl文件
一、编写type
二、编写Message
三、编写portType
四、编写bingding
五、编写service
[/size]

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:tns="http://www.example.org/my/"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
name="myServiceImplService" targetNamespace="http://www.example.org/my/">
<!-- TYPE -->
<wsdl:types>
<!-- 定义schema,通过tns引用该schema中的元素 -->
<xsd:schema targetNamespace="http://www.example.org/my/">
<xsd:element name="add" type="tns:addType"/>
<xsd:element name="addResponse" type="tns:addResponseType"/>

<xsd:complexType name="addType">
<xsd:sequence>
<xsd:element name="a" type="xsd:int"/>
<xsd:element name="b" type="xsd:int"/>
</xsd:sequence>
</xsd:complexType>

<xsd:complexType name="addResponseType">
<xsd:sequence>
<xsd:element name="addResult" type="xsd:int"/>
</xsd:sequence>
</xsd:complexType>

</xsd:schema>
</wsdl:types>

<!-- MESSAGE -->
<wsdl:message name="add">
<wsdl:part element="tns:add" name="parameters"/>
</wsdl:message>
<wsdl:message name="addResponse">
<wsdl:part element="tns:addResponse" name="parameters"/>
</wsdl:message>

<!-- PORTTYPE 指定接口和方法 -->
<wsdl:portType name="IMyservice">
<wsdl:operation name="add">
<wsdl:input message="tns:add"/>
<wsdl:output message="tns:addResponse"/>
</wsdl:operation>
</wsdl:portType>

<!-- BINDING 指定编码方式 -->
<wsdl:binding name="MyServiceImplPortBinding" type="tns:IMyservice">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="add">
<wsdl:input>
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>

<!--
SERVICE服务
注意:service name必须与wsdl definition中的name一致
-->
<wsdl:service name="MyServiceImplService">
<wsdl:port binding="tns:MyServiceImplPortBinding" name="MyServiceImplPort">
<!-- 指定服务发布的地址 -->
<soap:address location="http://localhost:7777/ms"/>
</wsdl:port>
</wsdl:service>

</wsdl:definitions>



[size=medium]
使用wsimport将wsdl转换为java代码
只保留IMyservice接口(利用接口以及定义好的注解-对参数的映射配置),其它都删除(因为服务端不用那些java文件)
编写IMyservice的实现类
[/size]

wsimport -d E:\technology-hqh\proj\webservice\JAX-WS\wsimport_03 -keep my.wsdl




package org.example.my;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.xml.bind.annotation.XmlSeeAlso;
import javax.xml.ws.RequestWrapper;
import javax.xml.ws.ResponseWrapper;
/**
* 接口
*/

/**
* This class was generated by the JAX-WS RI.
* JAX-WS RI 2.1.6 in JDK 6
* Generated source version: 2.1
*
*/
@WebService(name = "IMyservice", targetNamespace = "http://www.example.org/my/")
public interface IMyservice {


/**
*
* @param b
* @param a
* @return
* returns int
*/
@WebMethod
@WebResult(name = "addResult", targetNamespace = "")
@RequestWrapper(localName = "add", targetNamespace = "http://www.example.org/my/", className = "org.example.my.AddType")
@ResponseWrapper(localName = "addResponse", targetNamespace = "http://www.example.org/my/", className = "org.example.my.AddResponseType")
public int add(
@WebParam(name = "a", targetNamespace = "")
int a,
@WebParam(name = "b", targetNamespace = "")
int b);

}




package org.example.my;

import javax.jws.WebService;
/**
* 实现类
* 指定wsdlLocation="META-INF/wsdl/my.wsdl",使用本地以及编写好的wsdl文件
*/
@WebService(endpointInterface="org.example.my.IMyservice",
targetNamespace = "http://www.example.org/my/",
wsdlLocation="META-INF/wsdl/my.wsdl")
public class MyServiceImpl implements IMyservice {

@Override
public int add(int a, int b) {
System.out.println("MyServiceImpl.add()");
return a+b;
}

}


[b]开启服务端的服务[/b]

package org.example.my;

import javax.xml.ws.Endpoint;

public class MyServer {
public static void main(String[] args) {
String address = "http://localhost:7777/ms";
IMyservice implementor = new MyServiceImpl();
Endpoint.publish(address, implementor);
}

}



[b]客户端通过wsimport将发布的wsdl文件转换为本地的代码[/b]

wsimport -d E:\technology-hqh\proj\webservice\JAX-WS\wsimport_03 -keep http://localhost:7777/ms?wsdl


[b]客户端调用服务[/b]

package org.example.my.test;

import static org.junit.Assert.*;

import org.example.my.IMyservice;
import org.example.my.MyServiceImplService;


public class Test {

@org.junit.Test
public void test() {
MyServiceImplService serviceImpl = new MyServiceImplService();
IMyservice service = serviceImpl.getMyServiceImplPort();
int result = service.add(1, 2);
System.out.println(result);
}

}


[size=medium]
服务端发布新的wsdl,客户端必须根据wsdl更新本地代码
[/size]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值