1. 看看项目结构图
2. cxf的pom依赖
1 <dependency>
2 <groupId>org.apache.cxf</groupId>
3 <artifactId>cxf-spring-boot-starter-jaxws</artifactId>
4 <version>3.2.4</version>
5 </dependency>
3. 开始编写webService服务端
3.1 实体类entity
1 package com.example.demo.entity;
2
3 import java.io.Serializable;
4 /**
5 * @ClassName:User
6 * @Description:测试实体
7 * @author Jerry
8 * @date:2018年4月10日下午3:57:38
9 */
10 public class User implements Serializable{
11
12 private static final long serialVersionUID = -3628469724795296287L;
13
14 private String userId;
15 private String userName;
16 private String email;
17 public String getUserId() {
18 return userId;
19 }
20 public void setUserId(String userId) {
21 this.userId = userId;
22 }
23 public String getUserName() {
24 return userName;
25 }
26 public void setUserName(String userName) {
27 this.userName = userName;
28 }
29 public String getEmail() {
30 return email;
31 }
32 public void setEmail(String email) {
33 this.email = email;
34 }
35 @Override
36 public String toString() {
37 return "User [userId=" + userId + ", userName=" + userName + ", email=" + email + "]";
38 }
39
40 }
3.2 服务接口
1 package com.example.demo.service;
2
3 import javax.jws.WebMethod;
4 import javax.jws.WebParam;
5 import javax.jws.WebResult;
6 import javax.jws.WebService;
7
8 import com.example.demo.entity.User;
9 /**
10 * @ClassName:UserService
11 * @Description:测试服务接口类
12 * include:两个测试方法
13 * @author Jerry
14 * @date:2018年4月10日下午3:58:10
15 */
16 //@WebService(targetNamespace="http://service.demo.example.com")如果不添加的话,动态调用invoke的时候,会报找不到接口内的方法,具体原因未知.
17 @WebService(targetNamespace="http://service.demo.example.com")
18 public interface UserService {
19
20 @WebMethod//标注该方法为webservice暴露的方法,用于向外公布,它修饰的方法是webservice方法,去掉也没影响的,类似一个注释信息。
21 public User getUser(@WebParam(name = "userId") String userId);
22
23 @WebMethod
24 @WebResult(name="String",targetNamespace="")
25 public String getUserName(@WebParam(name = "userId") String userId);
26
27 }
3.3 服务接口的实现类
1 package com.example.demo.service.impl;
2
3 import java.util.HashMap;
4 import java.util.Map;
5 import java.util.UUID;
6
7 import javax.jws.WebService;
8
9 import org.springframework.stereotype.Component;
10
11 import com.example.demo.entity.User;
12 import com.example.demo.service.UserService;
13 /**
14 * @ClassName:UserServiceImpl
15 * @Description:测试服务接口实现类
16 * @author Jerry
17 * @date:2018年4月10日下午3:58:58
18 */
19 @WebService(serviceName="UserService",//对外发布的服务名
20 targetNamespace="http://service.demo.example.com",//指定你想要的名称空间,通常使用使用包名反转
21 endpointInterface="com.example.demo.service.UserService")//服务接口全路径, 指定做SEI(Service EndPoint Interface)服务端点接口
22 @Component
23 public class UserServiceImpl implements UserService{
24
25 private Map<String, User> userMap = new HashMap<String, User>();
26 public UserServiceImpl() {
27 System.out.println("向实体类插入数据");
28 User user = new User();
29 user.setUserId(UUID.randomUUID().toString().replace("-", ""));
30 user.setUserName("test1");
31 user.setEmail("Jerry@163.xom");
32 userMap.put(user.getUserId(), user);
33
34 user = new User();
35 user.setUserId(UUID.randomUUID().toString().replace("-", ""));
36 user.setUserName("test2");
37 user.setEmail("Jerryfix@163.xom");
38 userMap.put(user.getUserId(), user);
39
40 user = new User();
41 user.setUserId(UUID.randomUUID().toString().replace("-", ""));
42 user.setUserName("test3");
43 user.setEmail("Jerryfix@163.xom");
44 userMap.put(user.getUserId(), user);
45 }
46 @Override
47 public String getUserName(String userId) {
48 return "userId为:" + userId;
49 }
50 @Override
51 public User getUser(String userId) {
52 System.out.println("userMap是:"+userMap);
53 return userMap.get(userId);
54 }
55
56 }
3.4 发布webService的配置
1 package com.example.demo.config;
2
3 import javax.xml.ws.Endpoint;
4
5 import org.apache.cxf.Bus;
6 import org.apache.cxf.jaxws.EndpointImpl;
7 import org.apache.cxf.transport.servlet.CXFServlet;
8 import org.springframework.beans.factory.annotation.Autowired;
9 import org.springframework.boot.web.servlet.ServletRegistrationBean;
10 import org.springframework.context.annotation.Bean;
11 import org.springframework.context.annotation.Configuration;
12
13 import com.example.demo.service.UserService;
14 /**
15 * @ClassName:CxfConfig
16 * @Description:cxf发布webservice配置
17 * @author Jerry
18 * @date:2018年4月10日下午4:12:24
19 */
20 @Configuration
21 public class CxfConfig {
22 @Autowired
23 private Bus bus;
24
25 @Autowired
26 UserService userService;
27
28 /**
29 * 此方法作用是改变项目中服务名的前缀名,此处127.0.0.1或者localhost不能访问时,请使用ipconfig查看本机ip来访问
30 * 此方法被注释后:wsdl访问地址为http://127.0.0.1:8080/services/user?wsdl
31 * 去掉注释后:wsdl访问地址为:http://127.0.0.1:8080/soap/user?wsdl
32 * @return
33 */
34 //@SuppressWarnings("all") 不明白为什么必须给这个bean命名
35 @Bean(name="cxfServlet")
36 public ServletRegistrationBean dispatcherServlet() {
37 return new ServletRegistrationBean(new CXFServlet(), "/soap/*");
38 }
39
40 /** JAX-WS
41 * 站点服务
42 * **/
43 @Bean
44 public Endpoint endpoint() {
45 EndpointImpl endpoint = new EndpointImpl(bus, userService);
46 endpoint.publish("/user");
47 return endpoint;
48 }
49
50 }
4. 项目启动后的wsdl信息
由于图省事,我将项目的服务端口改为了80,这样就省去了IP后面写端口号的麻烦。
5. 两种调用方式
1 package com.example.demo.client;
2
3 import org.apache.cxf.endpoint.Client;
4 import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
5 import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
6
7 import com.example.demo.service.UserService;
8 /**
9 * @ClassName:CxfClient
10 * @Description:webservice客户端:
11 * 该类提供两种不同的方式来调用webservice服务
12 * 1:代理工厂方式
13 * 2:动态调用webservice
14 * @author Jerry
15 * @date:2018年4月10日下午4:14:07
16 */
17 public class CxfClient {
18
19
20 public static void main(String[] args) {
21 CxfClient.main1();
22 CxfClient.main2();
23 }
24
25 /**
26 * 1.代理类工厂的方式,需要拿到对方的接口地址
27 */
28 public static void main1() {
29 try {
30 // 接口地址
31 String address = "http://127.0.0.1/soap/user?wsdl";
32 // 代理工厂
33 JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean();
34 // 设置代理地址
35 jaxWsProxyFactoryBean.setAddress(address);
36 // 设置接口类型
37 jaxWsProxyFactoryBean.setServiceClass(UserService.class);
38 // 创建一个代理接口实现
39 UserService us = (UserService) jaxWsProxyFactoryBean.create();
40 // 数据准备
41 String userId = "maple";
42 // 调用代理接口的方法调用并返回结果
43 String result = us.getUserName(userId);
44 System.out.println("返回结果:" + result);
45 } catch (Exception e) {
46 e.printStackTrace();
47 }
48 }
49
50 /**
51 * 2:动态调用
52 */
53 public static void main2() {
54 // 创建动态客户端
55 JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
56 Client client = dcf.createClient("http://127.0.0.1/soap/user?wsdl");
57 // 需要密码的情况需要加上用户名和密码
58 // client.getOutInterceptors().add(new ClientLoginInterceptor(USER_NAME, PASS_WORD));
59 Object[] objects = new Object[0];
60 try {
61 // invoke("方法名",参数1,参数2,参数3....);
62 objects = client.invoke("getUserName", "maple");
63 System.out.println("返回数据:" + objects[0]);
64 } catch (java.lang.Exception e) {
65 e.printStackTrace();
66 }
67 }
68 }