JAX-WS搭建WebService和客户端访问程序
Jax-WS是新的WebService模式,难度已经比Xfire方式的降低了很多.
我的环境:myeclipse8.6+jdk1.6.0_29+tomcat6.0.37
一、搭建WebService
1.新建一个Web Service Project
2.新建一个Java类,写上一个接口方法,一会测试用
package com.ws.test; public class UserService { public String getUserName(String name){ return name; } }
3.New Web Service
4.点击"Next"后,点击Browse找到刚才新建的Java类,选中"Generate
WSDL in project"
5.添加JAX-WS类库至项目构建路径
6.部署项目到tomcat,访问url:http://localhost:8080/ws/UserServicePort?wsdl
<?xml version="1.0" encoding="UTF-8" ?> - <!-- Published by JAX-WS RI at http://jax-ws.dev.java.net. RI‘s version is JAX-WS RI 2.1.3-hudson-390-. --> - <!-- Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI‘s version is JAX-WS RI 2.1.3-hudson-390-. --> - <definitions xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://test.ws.com/" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="UserServiceService" targetNamespace="http://test.ws.com/"> - <types> - <xsd:schema> <xsd:import namespace="http://test.ws.com/" schemaLocation="http://localhost:8080/ws/UserServicePort?xsd=1" /> </xsd:schema> </types> - <message name="getUserName"> <part element="tns:getUserName" name="parameters" /> </message> - <message name="getUserNameResponse"> <part element="tns:getUserNameResponse" name="parameters" /> </message> - <portType name="UserServiceDelegate"> - <operation name="getUserName"> <input message="tns:getUserName" /> <output message="tns:getUserNameResponse" /> </operation> </portType> - <binding name="UserServicePortBinding" type="tns:UserServiceDelegate"> <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http" /> - <operation name="getUserName"> <soap:operation soapAction="" /> - <input> <soap:body use="literal" /> </input> - <output> <soap:body use="literal" /> </output> </operation> </binding> - <service name="UserServiceService"> - <port binding="tns:UserServicePortBinding" name="UserServicePort"> <soap:address location="http://localhost:8080/ws/UserServicePort" /> </port> </service> </definitions>
二、调用WebServices(确保webservice服务没停止后进行以下操作)
1.新建一个Java Project
2.New Web Service Client
3.点击finish后,可以看到目录结构如下图:
4.我们新建一个Java类来测试调用webservice
package com.ws.test; public class TestWs { public static void main(String[] args) { UserServiceService userServiceService = new UserServiceService(); UserServiceDelegate userServiceDelegate = userServiceService.getUserServicePort(); System.out.println(userServiceDelegate.getUserName("Gavin")); } }
输出结果为:Gavin
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。