栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

调用wsdl

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

调用wsdl

参考链接:java-webService(调用wsdl接口)

本文采用的是CXF调用webservice方式

准备工作 1.创建一个springboot工程

添加依赖:

     
            org.apache.cxf
            cxf-rt-frontend-jaxws
            3.1.16
        
        
            org.apache.cxf
            cxf-rt-transports-http-jetty
            3.1.16
        
        
            com.alibaba
            fastjson
            1.2.69
        

创建User实体

@Date
public class User {
    private String name;
    private int age;
}
发布webservice 1.创建TestWS类

随便写两方法

public class TestWS {

    public String getUser() {
        User user = new User("科比", 40);
        return JSONObject.toJSonString(user);
    }
    
    public String getMap(String name, int age) {
        Map map = new HashMap();
        map.put("name", name);
        map.put("age", age + "");
        map.put("mes", "你的姓名是" + name + ",年龄是" + age);
        return JSONObject.toJSonString(map);
    }

}
2.发布TestWS
    public static void main(String[] args) {
        SpringApplication.run(XXXApplication.class, args);

        //发布接口:实体类
        Endpoint.publish("http://localhost:8889/test", new TestWS());
    }

工程跑起来

可以访问这个链接查看webservice

http://localhost:8889/test?wsdl

在下图可以看下之前在TestWS写的两个方法

 调用webservice 1.测试接口
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("/web/ws")
public class TestController {

    @GetMapping("/testCB")
    public String testCB() {
        try {
            //刚刚访问的ws地址,注意这里是加wsdl的
            String urlWsdl = "http://localhost:8889/test?wsdl";
            String returnStr = null;
            JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
            Client client = dcf.createClient(urlWsdl);
            //getMap是方法,后面跟着的是入参
            Object[] result1 = client.invoke("getMap", "aa",11);
            if (result1 != null) {
                returnStr = (String) result1[0];
            }
            System.out.println("客户端:hello," + returnStr);
            return returnStr;
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return null;
        }
    }
}
2.测试结果

测试链接:localhost:8888/web/ws/testCB

使用工具:postman

请求方式:get

 

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/271253.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号