• JDK开发WebService


    java开发web service最简单的方式是用jdk6自带的支持web service的注解功能。 

    1、编写代码如下:

    package net.swiftlet;
    
    import javax.jws.WebService;
    import javax.xml.ws.Endpoint;
    
    //targetNamespace的默认值是service类所在包名,也可以设置成发布service的网站域名
    @WebService(serviceName = "HelloService", targetNamespace = "www.swiftlet.net")
    public class HelloService {
        public String sayhello(String name) {
            return "Hello " + name + " !";
    
        }
    
        public static void main(String[] args) {
            // 发布web service,参数1是服务公开的访问地址address,参数2是服务的具体实现
            Endpoint.publish("http://localhost:8080/hello", new HelloService());
        }
    }

    2、运行代码,一般以java application运行方式即可。可以看到运行后进程和启ServerSocket服务程序一样一直出于开启状态。输出信息如下:

    2015-4-2 20:05:16 com.sun.xml.internal.ws.model.RuntimeModeler getRequestWrapperClass
    信息: Dynamically creating request wrapper Class net.swiftlet.jaxws.Sayhello
    2015-4-2 20:05:16 com.sun.xml.internal.ws.model.RuntimeModeler getResponseWrapperClass
    信息: Dynamically creating response wrapper bean Class net.swiftlet.jaxws.SayhelloResponse

    3、在host系统文件里添加"127.0.0.1 www.swiftlet.net"。打开浏览器,输入"http://www.swiftlet.net:8080/hello",回车,此时页面可能是
    Web Services No JAX-WS context information available.
    因为一般浏览器不能直接访问WS程序,不过所有的WS都可以生成WSDL文档供一般浏览器访问,方式是在地址后面加上“?wsdl”就可以了,http://www.swiftlet.net:8080/hello?wsdl。
    生成的文档如下:

    <definitions targetNamespace="www.swiftlet.net" name="HelloService">
    <types><xsd:schema><xsd:import namespace="www.swiftlet.net" schemaLocation="http://www.swiftlet.net:8080/hello?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="HelloService">
    <operation name="sayhello">
    <input message="tns:sayhello"/>
    <output message="tns:sayhelloResponse"/>
    </operation></portType>
    <binding name="HelloServicePortBinding" type="tns:HelloService">
    <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="HelloService">
    <port name="HelloServicePort" binding="tns:HelloServicePortBinding"><soap:address location="http://www.swiftlet.net:8080/hello"/>
    </port>
    </service>
    </definitions>

    4 除了可以在浏览器上访问发布的service程序,还可以通过客户端来获取。如下代码: 

    package net.swiftlet;
    public class WebServiceClient
    {
    public static void main(String[] args)
    {
    HelloService myWebService = new HelloService();
    System.out.println(myWebService.sayhello("Swiftlet"));
    }
    }

    输出结果为:

    Hello Swiftlet !

    5 最后想说一点:HelloService类的@WebService注解部分可以简化。 

    这样就不用配置host:"127.0.0.1 www.swiftlet.net",可以使用此链接直接在浏览器进行访问:http://localhost:8080/hello?wsdl

  • 相关阅读:
    Luogu P4316 绿豆蛙的归宿 题解报告
    Luogu P1850 换教室(NOIP 2016) 题解报告
    Rainbow的信号 题解报告
    $CH5105 Cookies$ 线性$DP+$贪心
    算法竞赛 $0×50$ 动态规划 (+一本通
    $CH5104 I-country$ 线性$DP$
    洛谷$2014$ 选课 背包类树形$DP$
    $SP703 Mobile Service DP$
    $POJ1015 Jury Compromise Dp$/背包
    $POJ1742 Coins$ 多重背包+贪心
  • 原文地址:https://www.cnblogs.com/linjian/p/4674662.html
Copyright © 2020-2023  润新知