码迷,mamicode.com
首页 > Web开发 > 详细

WebService 实例

时间:2015-09-22 16:42:28      阅读:148      评论:0      收藏:0      [点我收藏+]

标签:

使用Java WebService API实现
1.服务端接口:
package com.h3c.itac.webservice;

import javax.jws.WebService;

@WebService  
public interface IServer {
    public String sayHello(String name);
}
2.服务端接口实现:
package com.h3c.itac.webservice;

import javax.jws.WebService;

@WebService(endpointInterface="com.h3c.itac.webservice.IServer")
public class Server implements IServer {

    public String sayHello(String name){
        System.out.println("hello "+name);
        return name;
    }
}

 

3.发布服务:
package com.h3c.itac.webservice;

import javax.xml.ws.Endpoint;

public class PublishServer {

    public static void main(String[] args){
        System.out.println("启动webservice服务!");
        Server server=new Server();
        Endpoint.publish("http://localhost:9090/web/myWebService", server);
        System.out.println("server 启动成功!");
    }
}
4.访问http://localhost:9090/web/myWebService,查看WebService信息

技术分享

 

5.点击http://localhost:9090/web/myWebService?wsdl 查看生成的wsdl:
This XML file does not appear to have any style information associated with it. The document tree is shown below.
<!--
 Published by JAX-WS RI at http://jax-ws.dev.java.net. RI‘s version is JAX-WS RI 2.2.4-b01. 
-->
<!--
 Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI‘s version is JAX-WS RI 2.2.4-b01. 
-->
<definitions xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsp="http://www.w3.org/ns/ws-policy" xmlns:wsp1_2="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://webservice.itac.h3c.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://webservice.itac.h3c.com/" name="ServerService">
<types>
<xsd:schema>
<xsd:import namespace="http://webservice.itac.h3c.com/" schemaLocation="http://localhost:9090/web/myWebService?xsd=1"/>
</xsd:schema>
</types>
<message name="sayHello">
<part name="parameters" element="tns:sayHello"/>
</message>
<message name="sayHelloResponse">
<part name="parameters" element="tns:sayHelloResponse"/>
</message>
<portType name="Server">
<operation name="sayHello">
<input wsam:Action="http://webservice.itac.h3c.com/Server/sayHelloRequest" message="tns:sayHello"/>
<output wsam:Action="http://webservice.itac.h3c.com/Server/sayHelloResponse" message="tns:sayHelloResponse"/>
</operation>
</portType>
<binding name="ServerPortBinding" type="tns:Server">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
<operation name="sayHello">
<soap:operation soapAction=""/>
<input>
<soap:body use="literal"/>
</input>
<output>
<soap:body use="literal"/>
</output>
</operation>
</binding>
<service name="ServerService">
<port name="ServerPort" binding="tns:ServerPortBinding">
<soap:address location="http://localhost:9090/web/myWebService"/>
</port>
</service>
</definitions>

 

6.客户端测试:
package com.h3c.itac.webservice;

import java.net.MalformedURLException;
import java.net.URL;

import javax.xml.namespace.QName;
import javax.xml.ws.Service;

import org.junit.Test;


public class TestClient {

    @Test
    public void testClient() throws MalformedURLException{
        QName qname=new QName("http://webservice.itac.h3c.com/","ServerService");
        URL url = new URL("http://localhost:9090/web/myWebService?wsdl");
        Service service = Service.create(url, qname);
        IServer server = service.getPort(IServer.class);
        String sayHello = server.sayHello("zhangsan");
        System.out.println("aaa   "+sayHello);
    }
}

 

7.输出:

  1).服务端:

技术分享

  2).客户端:

技术分享

 

WebService 实例

标签:

原文地址:http://www.cnblogs.com/wlf-919874006/p/4829043.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!