zoukankan      html  css  js  c++  java
  • 使用JAX-WS(JWS)发布WebService(一)

    1.     JAX-WS概述;
    2.     通过Main发布一个简单WebService;

        JAX-WS(Java API for XML Web Services)规范是一组XML web services的JAVA API,JAX-WS允许开发者可以选择RPC-oriented或者message-oriented 来实现自己的web services。

        在 JAX-WS中,一个远程调用可以转换为一个基于XML的协议例如SOAP,在使用JAX-WS过程中,开发者不需要编写任何生成和处理SOAP消息的代码。JAX-WS的运行时实现会将这些API的调用转换成为对应的SOAP消息。在服务器端,用户只需要通过Java语言定义远程调用所需要实现的接口SEI(service endpoint interface),并提供相关的实现,通过调用JAX-WS的服务发布接口就可以将其发布为WebService接口。在客户端,用户可以通过JAX-WS的API创建一个代理(用本地对象来替代远程的服务)来实现对于远程服务器端的调用。当然 JAX-WS 也提供了一组针对底层消息进行操作的API调用,你可以通过Dispatch 直接使用SOAP消息或XML消息发送请求或者使用Provider处理SOAP或XML消息。通过web service所提供的互操作环境,我们可以用JAX-WS轻松实现JAVA平台与其他编程环境(.net等)的互操作。

       使用JWS通过Main方法发布一个简单WebService:

    package com.ycdhz.web;
    
    import javax.jws.WebParam;
    import javax.jws.WebService;
    import javax.jws.soap.SOAPBinding;
    import javax.jws.soap.SOAPBinding.Style;
    
    @WebService(name = "HelloWS", targetNamespace = "http://www.tmp.com/ws/hello")  
    @SOAPBinding(style = Style.RPC)
    public interface HelloWService {
        public int sendMT(@WebParam(name = "User_ID") String User_ID, @WebParam(name = "Message") String Message,
                @WebParam(name = "Service_ID") String Service_ID);
    }
    package com.ycdhz.web;
    
    import javax.jws.WebService;
    
    @WebService(endpointInterface = "com.ycdhz.web.HelloWService", portName = "HelloWSPort", serviceName = "HelloWSService", targetNamespace = "http://www.tmp.com/ws/hello")
    public class HelloWServiceImpl implements HelloWService {
    
        public int sendMT(String User_ID, String Message, String Service_ID) {
            System.out.print(Service_ID+": "+User_ID+": "+Message);
            return 0;
        }
    }
    package com.ycdhz.web;
    
    import javax.xml.ws.Endpoint;
    
    public class HelloWSTest {
        
        public static void main(String[] args) {
            HelloWService helloWS = new HelloWServiceImpl();
            String address = "http://localhost:8080/HelloWS";
            Endpoint.publish(address, helloWS);
        }
    }

        在浏览器中通过http://localhost:8080/HelloWS可以成功访问这个web服务!

  • 相关阅读:
    清北学堂(2019 5 3) part 6
    清北学堂(2019 5 2) part 5
    清北学堂(2019 5 1) part 4
    dijkstra
    清北学堂(2019 4 30 ) part 3
    2020/5/1
    2020/4/30
    2020/4/29
    HSV模型
    2020/4/28
  • 原文地址:https://www.cnblogs.com/jiangyaxiong1990/p/9022272.html
Copyright © 2011-2022 走看看