JEECG使用CXF提供WebService服务

JEECG使用CXF提供WebService服务

一、软件版本

  • JEECG版本:4.0
  • 其它

JDK版本:1.8

开发工具:IntelliJ IDEA

MySQL版本:5.6.43

Apache CXF官网:https://cxf.apache.org/

二、JEECG使用CXF

1.项目配置

  • 1.下载源码:https://gitee.com/jeecg/jeecg
  • 2.打开 pom.xml 文件,放开注释
<!-- cxf webservice (暂时注释掉提高平台性能,需要此功能,可以放开注释)-->
<dependency> 
	<groupId>org.apache.cxf</groupId> 
	<artifactId>cxf-rt-frontend-jaxws</artifactId> 
	<version>${cxf.version}</version> 
</dependency> 
<dependency> 
	<groupId>org.apache.cxf</groupId> 
	<artifactId>cxf-rt-transports-http</artifactId> 
	<version>${cxf.version}</version> 
</dependency>
  • 3.打开 src/main/resources/spring-mvc.xml 文件,放开注释
<!--集成cxf webservice接口(暂时注释掉提高平台性能,需要此功能,可以放开注释)-->
<import resource="classpath:cxf-servlet.xml" />
  • 4.打开 src/main/webapp/WEB-INF/web.xml 文件,放开注释
<!-- 集成cxf webservice接口(暂时注释掉提高平台性能,需要此功能,可以放开注释)-->
<servlet>  
	<description>Apache CXF Endpoint</description>  
    <display-name>cxf</display-name>  
    <servlet-name>cxf</servlet-name>  
    <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>  
    <load-on-startup>2</load-on-startup>  
</servlet>  
<servlet-mapping>  
    <servlet-name>cxf</servlet-name>  
    <url-pattern>/cxf/*</url-pattern>  
</servlet-mapping>

2.测试代码

1) 创建目录结构
  • src/main/java/com.jeecg.demo/webservice

  • client

  • server

    • impl
2) 创建服务端接口
  • webservice/server/ITestWebService.java

  • 注意:接口需要添加注解 @WebService

package com.jeecg.demo.webservice.server;

import javax.jws.WebService;

@WebService
public interface ITestWebService {

    public String sayHello();

    public String sayHello2(String content);

    public String sayHello3(String name, String content);
}

3) 创建服务端实现类
  • webservice/server/impl/TestWebServiceImpl.java

  • 注意:由于 接口实现类 是不同目录,所以需要使用 @WebServicetargetNamespace 属性,值是 http:// + 接口目录的反写

    • 如果 接口实现类 相同目录,不需要添加注解 @WebService

    • 如果 接口实现类 不同目录,又没使用 @WebServicetargetNamespace 属性,客户端调用时,会报错:

    No operation was found with the name {http://impl.server.webservice.demo.jeecg.com/}sayHello.

package com.jeecg.demo.webservice.server.impl;

import com.jeecg.demo.webservice.server.ITestWebService;
import javax.jws.WebService;

@WebService(targetNamespace = "http://server.webservice.demo.jeecg.com/")
public class TestWebServiceImpl implements ITestWebService {

    @Override
    public String sayHello() {
        return "sayHello方法返回:Hello";
    }

    @Override
    public String sayHello2(String content) {

        return "sayHello2方法返回:"+content;
    }

    @Override
    public String sayHello3(String name, String content) {

        return "sayHello3方法返回:"+name+" --> "+content;
    }
}
4) 配置服务端点
  • 打开 jeecg/src/main/resources/cxf-servlet.xml 文件,在 配置下方添加配置
<!-- TestWebService -->
<jaxws:endpoint id="testWebService"
		implementor="com.jeecg.demo.webservice.server.impl.TestWebServiceImpl"
		address="/TestWebService" />

浏览器测试:http://localhost:8090/cxf/TestWebService?wsdl

5) 创建客户端测试类
  • webservice/client/TestClient.java
package com.jeecg.demo.webservice.client;

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

public class TestClient {

    public static void main(String[] args) {
        String wsdlURL = "http://localhost:8090/cxf/TestWebService?wsdl";
        String method = "sayHello";
        String param = "";

        String method2 = "sayHello2";
        String param2 = "Zcheng";

        String method3 = "sayHello3";
        String param3 = "你好!";
        try {
            // sayHello
            Object [] object = connCXFServer(wsdlURL, method, param);
            System.out.println(object[0].toString());
            // sayHello2
            object = connCXFServer(wsdlURL, method2, param2);
            System.out.println(object[0].toString());
            // sayHello3
            object = connCXFServer(wsdlURL, method3, param2, param3);
            System.out.println(object[0].toString());
        } catch (Exception e) {
            //e.printStackTrace();
            System.out.println("调用失败,出现异常:"+e.getMessage());
        }
    }

    /**
     * 调用WSDL
     * */
    public static Object[] connCXFServer(String wsdlURL, String method, Object... params) throws Exception {
        JaxWsDynamicClientFactory jaxWsDynamicClientFactory 
            = JaxWsDynamicClientFactory.newInstance();
        Client client = jaxWsDynamicClientFactory.createClient(wsdlURL);
        return client.invoke(method, params);
    }

}

三、其它

  • 以上 jeecg 使用的是 spring + cxf 的方式进行项目构建

1.单独 cxf 使用

  • 1 ) 创建简单的maven java程序

  • 2 ) pom.xml 添加依赖

<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>apache-cxf</artifactId>
    <version>3.2.4</version>
</dependency>
  • 3 ) 创建 CXF 服务类
package com.test.cxf.server;

import javax.jws.WebService;

@WebService
public class CXFWebService {

    public String sayHello(String content){

        return "sayHello: "+ content;
    }

}
  • 4 ) 创建 CXF 服务发布类
package com.test.cxf.server;

import org.apache.cxf.endpoint.Server;
import org.apache.cxf.jaxws.JaxWsServerFactoryBean;

public class ServerPublisher {

    public static void main(String[] args) {
        String serverUrl = "http://localhost:8085/cxf/hello";
        JaxWsServerFactoryBean jaxWsServerFactoryBean 
            = new JaxWsServerFactoryBean();
        jaxWsServerFactoryBean.setServiceClass(CXFWebService.class);
        jaxWsServerFactoryBean.setAddress(serverUrl);
        Server server = jaxWsServerFactoryBean.create();
        server.start();
    }

}

浏览器查看:http://localhost:8085/cxf/hello?wsdl

  • 5 ) 编写 CXF 客户端类
package com.test.cxf.client;

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

public class CXFClient {

    public static void main(String[] args) {
        String wsdlURL = "http://localhost:8085/cxf/hello?wsdl";
        String method = "sayHello";
        String param = "你好!";
        try {
            // sayHello
            Object [] object = connCXFServer(wsdlURL, method, param);
            System.out.println(object[0].toString());
        } catch (Exception e) {
            //e.printStackTrace();
            System.out.println("调用失败,出现异常:"+e.getMessage());
        }
    }

    /**
     * 调用WSDL
     * */
    public static Object[] connCXFServer(String wsdlURL, String method, Object... params) throws Exception {
        JaxWsDynamicClientFactory jaxWsDynamicClientFactory 
            = JaxWsDynamicClientFactory.newInstance();
        Client client = jaxWsDynamicClientFactory.createClient(wsdlURL);
        return client.invoke(method, params);
    }

}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

趴着喝可乐

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值