基于Jax-ws的几种访问服务的方式

方式1:
可以看到在WSDL中会有一个

<wsdl:portType name="CarShop">
  ......................
</wsdl:portType>
<service name="CarShopService">
	    <port name="CarShopPort" binding="tns:CarShopBinding">
			<soap:address location="http://cccc.eb.com/xmlapi/shop/car/v1"/>
	    </port>
	</service>


通过编译会自动生成所有的类型,其中有一个名为CarShopService的一个class,这就代表我们发布的服务。
如下是字节码文件的头:
@javax.xml.ws.WebServiceClient(name="CarShopService",
  targetNamespace="urn:expedia:wsapi:car:v1",
  wsdlLocation="com.expedia.wsapi.car.v1.wsdl")
public class com.wsapi.car.v1.CarShopService extends javax.xml.ws.Service {

通过以下这个代码我们可以在任意的地方访问这个服务。
WebServiceClient annotation = 
				CarShopService.class.getAnnotation(WebServiceClient.class);
			
			wsdlUrl = new URL(CarShopService.class.getResource(
					CarShopService.class.getSimpleName() + ".class"), 
					annotation.wsdlLocation());
			
			qName = new QName(annotation.targetNamespace(), annotation.name());
m_service = new CarShopService(wsdlUrl, qName);
		m_port = m_service.getCarShopPort();


方式2:
URL createOrderURL = new URL(m_client.getEndpoint() + "?wsdl");
			QName qName = new QName("urn:com:shop:car:v1", "CarShopService");
			CarShopServiceo cs = new CarShopService(buyCarURL, buyCarQName);
			CarShopPort port = cs.get****();

m_client.getEndpoint():是获取这个Service部署在哪里,例如:
http://localhost:8080/CarShopService
这里就不再贴代码了。
两种方式都要创建服务,然后获取这个服务中的具体服务提供者(port).运用起来也很方便,让我们在任意的地方,只要有这个web服务存在,我们就可以通过这2种方式,去访问并调用。

猜你喜欢

转载自cyril0513.iteye.com/blog/1714587