JAX-WS搭建WebService和客户端访问程序

Jax-WS是新的WebService模式,难度已经比Xfire方式的降低了很多.
我的环境:myeclipse8.6+jdk1.6.0_29+tomcat6.0.37

一、搭建WebService
1.新建一个Web Service Project
JAX-WS搭建WebService和客户端访问程序
2.新建一个Java类,写上一个接口方法,一会测试用

JAX-WS搭建WebService和客户端访问程序
package com.ws.test;

public class UserService {

    public String getUserName(String name){
        return name;
    }
    
}
JAX-WS搭建WebService和客户端访问程序

3.New Web Service
JAX-WS搭建WebService和客户端访问程序
4.点击"Next"后,点击Browse找到刚才新建的Java类,选中"Generate WSDL in project"
JAX-WS搭建WebService和客户端访问程序
5.添加JAX-WS类库至项目构建路径
JAX-WS搭建WebService和客户端访问程序
6.部署项目到tomcat,访问url:http://localhost:8080/ws/UserServicePort?wsdl

JAX-WS搭建WebService和客户端访问程序
  <?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>
View Code


二、调用WebServices
(确保webservice服务没停止后进行以下操作)

1.新建一个Java Project
2.New Web Service Client
JAX-WS搭建WebService和客户端访问程序
JAX-WS搭建WebService和客户端访问程序
3.点击finish后,可以看到目录结构如下图:
JAX-WS搭建WebService和客户端访问程序
4.我们新建一个Java类来测试调用webservice

JAX-WS搭建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"));
    }
}
JAX-WS搭建WebService和客户端访问程序

输出结果为:Gavin
 

JAX-WS搭建WebService和客户端访问程序

上一篇:windows和linux双系统修改启动项


下一篇:【ASP.NET Web API教程】2.3 与实体框架一起使用Web API