SpringBoot整合cxf发布webService

简介: 1. 看看项目结构图2. cxf的pom依赖1 2 org.apache.cxf3 cxf-spring-boot-starter-jaxws4 3.2.45 3.

1. 看看项目结构图
SpringBoot整合cxf发布webService

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 服务接口

package com.example.demo.service;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;

import com.example.demo.entity.User;
/

}**

3.3 服务接口的实现类

package com.example.demo.service.impl;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import javax.jws.WebService;

import org.springframework.stereotype.Component;

import com.example.demo.entity.User;
import com.example.demo.service.UserService;
/

}****

3.4 发布webService的配置

package com.example.demo.config;

import javax.xml.ws.Endpoint;

import org.apache.cxf.Bus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.apache.cxf.transport.servlet.CXFServlet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.example.demo.service.UserService;
/**

}

4. 项目启动后的wsdl信息

SpringBoot整合cxf发布webService

由于图省事,我将项目的服务端口改为了80,这样就省去了IP后面写端口号的麻烦。

5. 两种调用方式

package com.example.demo.client;

import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;

import com.example.demo.service.UserService;
/**

  • @ClassName:CxfClient
  • @Description:webservice客户端:
  • 该类提供两种不同的方式来调用webservice服务
  • 1:代理工厂方式
  • 2:动态调用webservice
  • @author Jerry
  • @date:2018年4月10日下午4:14:07
    */
    public class CxfClient {

    public static void main(String[] args) {
    CxfClient.main1();
    CxfClient.main2();
    }

    /**

    • 1.代理类工厂的方式,需要拿到对方的接口地址
      */
      public static void main1() {
      try {
      // 接口地址
      String address = "http://127.0.0.1/soap/user?wsdl";
      // 代理工厂
      JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean();
      // 设置代理地址
      jaxWsProxyFactoryBean.setAddress(address);
      // 设置接口类型
      jaxWsProxyFactoryBean.setServiceClass(UserService.class);
      // 创建一个代理接口实现
      UserService us = (UserService) jaxWsProxyFactoryBean.create();
      // 数据准备
      String userId = "maple";
      // 调用代理接口的方法调用并返回结果
      String result = us.getUserName(userId);
      System.out.println("返回结果:" + result);
      } catch (Exception e) {
      e.printStackTrace();
      }
      }

    /**

    • 2:动态调用
      */
      public static void main2() {
      // 创建动态客户端
      JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
      Client client = dcf.createClient("http://127.0.0.1/soap/user?wsdl");
      // 需要密码的情况需要加上用户名和密码
      // client.getOutInterceptors().add(new ClientLoginInterceptor(USER_NAME, PASS_WORD));
      Object[] objects = new Object[0];
      try {
      // invoke("方法名",参数1,参数2,参数3....);
      objects = client.invoke("getUserName", "maple");
      System.out.println("返回数据:" + objects[0]);
      } catch (java.lang.Exception e) {
      e.printStackTrace();
      }
      }
      }

6. 注意点.
诚如之前所说,如果接口的注解上不加targetNamespace的话,动态调用的时候,会报如下的错误。

SpringBoot整合cxf发布webService

目录
相关文章
|
2月前
|
Java 数据处理
【十二】springboot整合WebService
【十二】springboot整合WebService
52 0
|
10月前
phpstorm插件应用:Test RESTful WEB Service 控制台接口调试工具
phpstorm插件应用:Test RESTful WEB Service 控制台接口调试工具
119 0
|
2月前
|
Java
【十三】springboot整合WebService关于传参数
【十三】springboot整合WebService关于传参数
20 0
|
2月前
|
存储 缓存 算法
关于 Service Worker 和 Web 应用对应关系的讨论
关于 Service Worker 和 Web 应用对应关系的讨论
14 0
|
3月前
|
Java API Apache
Apache CXF生成WebService的客户端
Apache CXF生成WebService的客户端
|
7月前
|
JSON 安全 API
使用 ABAP sproxy 事务码生成的 Proxy 消费 Web Service
使用 ABAP sproxy 事务码生成的 Proxy 消费 Web Service
56 0
|
3月前
|
XML 网络架构 数据格式
Ruby 教程 之 Ruby Web Service 应用 - SOAP4R 2
Ruby Web Service 应用 - SOAP4R
24 5
|
3月前
|
XML Linux 网络架构
Ruby 教程 之 Ruby Web Service 应用 - SOAP4R 1
Ruby Web Service 应用 - SOAP4R
23 3
|
9月前
|
XML Java API
Java Web Service Get请求使用指南
Java Web Service Get请求使用指南 在当今互联网时代,Web Service已经成为了现代软件开发中不可或缺的一部分。而Java作为一种广泛使用的编程语言,自然也提供了丰富的工具和库来支持Web Service的开发。本文将为大家介绍如何使用Java编程语言进行Web Service的Get请求。
89 0
|
5月前
|
Java 数据库连接 Apache
SpringBoot整合CXF实现WebService
SpringBoot整合CXF实现WebService
127 0