参考链接:java-webService(调用wsdl接口)
本文采用的是CXF调用webservice方式
准备工作 1.创建一个springboot工程添加依赖:
org.apache.cxf cxf-rt-frontend-jaxws3.1.16 org.apache.cxf cxf-rt-transports-http-jetty3.1.16 com.alibaba fastjson1.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



