zoukankan      html  css  js  c++  java
  • 记录webservice

    公司的一个老项目,定义了接口,供其他应用访问.定义的方式就是webservice.

    我这边的环境是springboot.

    首先引入依赖jar

        

    声明一个服务端. @WebSerevice注解中name则是对外暴露的服务.

    被@WebMethod 注解的方法,则是webservice对外暴露的方法.

    import com.alibaba.fastjson.JSON;
    import com.prologint.waybill.api.ParameterEntity.GetWaybillYTRequest;
    import com.prologint.waybill.api.ParameterEntity.GetWaybillYTResponse;
    import com.prologint.waybill.service.GetWaybillYTService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Component;
    import javax.jws.WebMethod;
    import javax.jws.WebService;
    
    @WebService(name = "WaybillYT")
    @Component
    public class WaybillYTService {
    

      @WebMethod public String getWaybillYT(String billNo, String branchId, String consignor) { return "ok"; } }

    配置webservice.

    import javax.xml.ws.Endpoint;
    
    import org.apache.cxf.Bus;
    import org.apache.cxf.jaxws.EndpointImpl;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    @Configuration
    public class WaybillYTServiceConfig {
        @Autowired
        private Bus bus;
    
        @Autowired
        private WaybillYTService waybillYTService;
    
        /** JAX-WS **/
        @Bean
        public Endpoint endpoint() {
            EndpointImpl endpoint = new EndpointImpl(bus, waybillYTService);
            endpoint.publish("/WaybillYT");
            return endpoint;
        }
    }

    客户端测试,端口替换为自己的服务器port即可

    import org.apache.cxf.endpoint.Client;
    import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
    
    public class Cilent {
        public static void main(String[] args) {
            // 创建动态客户端
            JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
            Client client = dcf.createClient("http://localhost:80/services/WaybillYT?wsdl");
            Object[] objects = new Object[0];
            try {
                // invoke("方法名",参数1,参数2,参数3....);
                objects = client.invoke("getWaybillYT", "111111","222222222","3333333333");
                System.out.println("返回数据:" + objects[0]);
            } catch (java.lang.Exception e) {
                e.printStackTrace();
            }
        }
    }
  • 相关阅读:
    剑指Offer-30.连续子数组的最大和(C++/Java)
    剑指Offer-29.最小的K个数(C++/Java)
    UVA 1616 Caravan Robbers 商队抢劫者(二分)
    UVA 10570 Meeting with Aliens 外星人聚会
    UVA 11093 Just Finish it up 环形跑道 (贪心)
    UVA 12673 Erratic Expansion 奇怪的气球膨胀 (递推)
    UVA 10954 Add All 全部相加 (Huffman编码)
    UVA 714 Copying Books 抄书 (二分)
    UVALive 3523 Knights of the Round Table 圆桌骑士 (无向图点双连通分量)
    codeforecs Gym 100286B Blind Walk
  • 原文地址:https://www.cnblogs.com/zumengjie/p/11497177.html
Copyright © 2011-2022 走看看