sprinBoot+gradle整合cxf发布webservice入门案例

本文介绍了如何使用SpringBoot和Gradle整合Apache CXF来创建和消费WebService服务。首先,创建两个SpringBoot项目,并在build.gradle中添加相关依赖。然后,详细阐述了服务端的创建步骤,包括定义UserService接口,实现类UserServiceImpl,以及配置发送服务类SendServer。接着,讲解了客户端的创建,包括client_demo类的构建。文章还强调了一些注意事项,如JDK 1.8中jaxp.properties文件的重要性,版本匹配,服务路径的自定义,以及@WebService注解的相关规则。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

]

一.创建两个springBoot+gradle项目,将以下依赖放入build.gradle中

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    //引入cxf框架
    compile 'org.apache.cxf:cxf-spring-boot-starter-jaxws:3.2.6'
}

二.首先创建webService服务端

1.创建一个UserService接口(注意加上@webService才能成为服务端)

@WebService(name = "UserService", // 暴露服务名称
            targetNamespace = "http://service.wsserver.meng.com" // 命名空间,一般是接口的包名倒序
       )
public interface UserService {
    @WebMethod//标注该方法为webservice暴露的方法,用于向外公布,它修饰的方法是webservice方法,去掉也没影响的,类似一个注释信息。
    @WebResult(name="int",targetNamespace="")//name属性为返回值类型,targetNamespace可以为空
    //模拟两个数相加
    public int add(@WebParam(name = "w1")int w1,@WebParam(name = "w2")int w2);
    
}     

2.创建UserService的实现类UserServiceImpl

@WebService(serviceName="UserService",//对外发布的服务名
        targetNamespace = "http://service.wsserver.meng.com",//指定你想要的名称空间,通常使用使用包名反转
        endpointInterface="com.meng.wsserver.service.UserService")//服务接口全路径, 指定做SEI(Service EndPoint Interface)服务端点接口
@Component
public class UserServiceImpl implements UserService {
  //业务逻辑
    @WebMethod(operationName = "add") // 指定方法名
    @Override
    public int add(int w1, int w2) {
        return w1+w2;
    }
}

3.创建配置发布服务类SendServer

@Configuration
public class SendServer {
    @Autowired
    private Bus bus;

    @Autowired
    UserService userService;
    /**
     * 此方法作用是改变项目中服务名的前缀名,此处127.0.0.1或者localhost不能访问时,请使用ipconfig查看本机ip来访问
     * 此方法被注释后:wsdl访问地址为http://127.0.0.1:8080/services/user?wsdl
     * 去掉注释后:wsdl访问地址为:http://127.0.0.1:8080/soap/user?wsdl
     * 
     *  或者 CXF默认的服务路径是"/services/**"。如果你想改变它的url直接在在application.properties中修改     cxf.path=/soap
     */
    @SuppressWarnings("all")
    @Bean
    public ServletRegistrationBean dispatcherServlet() {
        return new ServletRegistrationBean(new CXFServlet(), "/soap/*");
    }
    /** JAX-WS
     * 站点服务
     * **/
    @Bean
    public Endpoint endpoint() {
        EndpointImpl endpoint = new EndpointImpl(bus, userService);
        endpoint.publish("/user");
        return endpoint;
    }
}

二.创建webService客户端

1.创建客户端类client_demo

package com.hao.c_demo.c_test;

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


public class client_demo {

    public static void main(String[] args) throws Exception{

//采用动态工厂方式 不需要指定服务接口
        JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
        Client client =  dcf.createClient("http://127.0.0.1:8080/soap/user?wsdl");
        Object[] objects = new Object[0];
        try {
            // invoke("方法名",参数1,参数2,参数3....);
            objects = client.invoke("add", 1,2);
            System.out.println("返回数据:" + objects[0]);
        } catch (java.lang.Exception e) {
            e.printStackTrace();
        }

    }

}


/*
       第二种方式 需要指定命名空间
        JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
        Client client = dcf
        .createClient("http://127.0.0.1:8080/soap/user?wsdl");//参数为服务端发布的地址
        QName qName = new QName("http://service.wsserver.meng.com", "add");//QName就是创建xml的元素,第一个参数为方法的命名空间,第二个为方法名
        try {
        Object[] objects = new Object[0];
        // invoke("方法名",参数1,参数2,参数3....);
        objects =client.invoke(qName,1,2);
        System.out.println(objects[0]);
        } catch (Exception e) {
        e.printStackTrace();
        }
        */

2. 查看运行结果:

运行结果页面

三.需要注意的问题

1. 如果使用的是jdk1.8则需要看一下在jdk1.8.0/jre/lib目录下有没有一个jaxp.properties文件,如果没有则创建一个,其文件内容为 javax.xml.accessExternalSchema = all

否则会报错(java.lang.AssertionError: org.xml.sax.SAXParseException; systemId: jar:file:/path/to/glassfish/modules/jaxb-osgi.jar!/com/sun/tools/xjc/reader/xmlschema/bindinfo/binding.xsd; lineNumber: 52; columnNumber: 88; schema_reference: Failed to read schema document ‘xjc.xsd’, because ‘file’ access is not allowed due to restriction set by the accessExternalSchema property)

2.使用springboot结合cfx框架时要对应好版本号

parent版本是1.4.5,同时jaxws版本是3.1.7时;
parent版本是1.4.5,同时jaxws版本是3.1.11时;
parent版本是1.5.6,同时jaxws版本是3.1.11时;
parent版本是1.5.8,同时jaxws版本是3.1.12时;
parent版本是1.5.9,同时jaxws版本是3.1.11时;
parent版本是1.5.9,同时jaxws版本是3.1.12时;
parent版本是2.0.3,同时jaxws版本是3.2.6时;
spring boot 1.4 版本对应cxf-spring-boot-starter-jaxws   3.1.X  版本
spring boot 1.5 版本对应cxf-spring-boot-starter-jaxws   3.2.X  版本

3. CXF默认的服务路径是"/services/**"。如果你想改变它的url为/xxx/*。在application.properties中修改

 cxf.path=/xxx

4.注解及其解释

@WebService (serviceName="PhoneManager",//修改服务名
	targetNamespace="http://dd.ws.it.cn") //修改命名空间 ,默认包名,取反
	//声明该业务类 对外提供webservice服务   ,默认只是对public 修饰的方法对外以webservice形式发布
	

    public class PhoneService {
    
    	/**@WebMethod(operationName="getMObileInfo"): 修改方法名
    		 * @WebResult(name="phone"):修改返回参数名
    		 * @WebParam(name="osName"):修改输入参数名
    		 */
    	}

5. 在类上添加@WebService注解,代表发布一个WebService服务

6. protected、private、final、static方法不能对外公开

7. 通过EndPoint(端点服务)发布一个webService。Endpoint也是jdk提供的一个专门用于发布服务的类,它的publish方法接收两个参数,一个是本地的服务地址,二是提供服务的类。.

8. Endpoint.publish(String address, Object implementor) 静态方法在给定地址处针对指定的实现者对象创建并发布端点

9. 给类添加上@WebService注解后,类中所有的非静态方法都将会对外公布

10. 如果希望某个方法不对外公开,可以在方法上添加 @WebMethod(exclude=true),阻止对外公开。

11. 如果一个类上,被添加了@WebService注解,则必须此类至少有一个可以公开的方法,否则将会启动失败。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值