Webservice

springboot使用webservice

  • 基于Apache CXF(3.1.8)Springboot(1.5.6.RELEASE)测试
  • 引入依赖
1
2
3
4
5
6
7
8
9
10
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.1.8</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.1.8</version>
</dependency>

服务端

  • 接口地址:http://localhost:8080/services/user?wsdl,服务描述如下

    webservice

  • 主要代码如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// 1.Model, 省略getter/setter
public class User implements Serializable {
private static final long serialVersionUID = -5939599230753662529L;
private String userId;
private String username;
private String age;
private Date updateTime;
// ... 省略getter/setter
}

// 2.服务接口
@WebService // import javax.jws.WebService;
public interface UserService {
@WebMethod
String getName(@WebParam(name = "userId") String userId);

@WebMethod
User getUser(String userId);

@WebMethod
ArrayList<User> getAllUser();
}

// 3.服务实现
@WebService(
serviceName = "UserServiceWeb", // 服务名(默认UserServiceImplService)
targetNamespace = "http://service.cxf.webservice.springboot.aezo.cn/", // 实现类包名倒写 (默认,可省略,基于JAX-WS调用服务会用到)
endpointInterface = "cn.aezo.springboot.webservice.cxf.service.UserService" // 接口的全路径
)
@Service
public class UserServiceImpl implements UserService {
private Map<String, User> userMap = new HashMap<>();

public UserServiceImpl() {
User user = new User();
user.setUserId("1");
user.setUsername("zhansan");
user.setAge("20");
user.setUpdateTime(new Date());
userMap.put(user.getUserId(), user);

user = new User();
user.setUserId("2");
user.setUsername("lisi");
user.setAge("30");
user.setUpdateTime(new Date());
userMap.put(user.getUserId(), user);

user = new User();
user.setUserId("3");
user.setUsername("wangwu");
user.setAge("40");
user.setUpdateTime(new Date());
userMap.put(user.getUserId(), user);
}

@Override
public String getName(String userId) {
return "id-" + userId;
}
@Override
public User getUser(String userId) {
User user = userMap.get(userId);
return user;
}

@Override
public ArrayList<User> getAllUser() {
ArrayList<User> users = new ArrayList<>();
userMap.forEach((key,value) -> {
users.add(value);
});
return users;
}
}

// 4.配置
@Configuration
public class WebServiceConfig {
@Bean
public ServletRegistrationBean dispatcherServlet() {
return new ServletRegistrationBean(new CXFServlet(),"/services/*"); // 发布服务名称
}

@Bean(name = Bus.DEFAULT_BUS_ID)
public Bus springBus() { // import org.apache.cxf.Bus;
return new SpringBus(); // import org.apache.cxf.bus.spring.SpringBus;
}

@Bean
public UserService userService() {
return new UserServiceImpl();
}

@Bean
public Endpoint endpoint() {
EndpointImpl endpoint = new EndpointImpl(springBus(), userService()); // 绑定要发布的服务
endpoint.publish("/user"); // 显示要发布的名称
return endpoint;
}
}

客户端

  • 生成客户端代码
    • 进入目录D:\java\apache-cxf-3.1.8\bin
    • 运行wsdl2java -p cn.aezo.springboot.webservice.cxf.client.cxf -d D:\gitwork\springboot\webservice\src\main\java -encoding utf-8 -client http://localhost:8080/services/user?wsdl
    • cxf(wsdl2java) 和 jdk(wsimport)都可生成,且调用方式一致
  • 调用
1
2
3
4
5
6
7
8
9
cn.aezo.springboot.webservice.cxf.client.cxf.UserServiceWeb ss =
// new cn.aezo.springboot.webservice.cxf.client.cxf.UserServiceWeb(); // 此时也可以调用成功
new cn.aezo.springboot.webservice.cxf.client.cxf.UserServiceWeb(
new URL("http://localhost:8080/services/user?wsdl"), // wsdlURL
new QName("http://service.cxf.webservice.springboot.aezo.cn/", "UserServiceWeb") // SERVICE_NAME
);
cn.aezo.springboot.webservice.cxf.client.cxf.UserService port = ss.getUserServiceImplPort();
String username = port.getName("1");
System.out.println("====================>username = " + username); // id-1

OFBiz使用webservice

ChatGPT开源小程序