SpringBoot + CXF 开发Web Service 服务

本文介绍如何在Spring Boot项目中使用CXF框架搭建WebService服务,并提供具体配置及调用示例。

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

首先是POM.XML 

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.company</groupId>
  <artifactId>project-name</artifactId>
  <packaging>jar</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>endtoend Maven Webapp</name>
  <url>http://maven.apache.org</url>
  
  <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.4.RELEASE</version>
    </parent>
    
    
    <properties>
    	<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<cxf.version>3.1.12</cxf.version>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
		<tomcat.version>7.0.69</tomcat.version>
		<activiti-version>5.22.0</activiti-version>
		<spring.version>4.3.4.RELEASE</spring.version>
		<mybatis.version>3.4.1</mybatis.version>
		<org.mybatis.spring.boot.version>1.1.1</org.mybatis.spring.boot.version>
		<mysql.version>5.1.34</mysql.version>
		<oracle.version>11.2.0.4</oracle.version>
		
		<slf4j.version>1.7.21</slf4j.version>
		<log4j2.version>2.7</log4j2.version>
		<druid.version>1.0.26</druid.version>
		<commonsfileupload.version>1.3.2</commonsfileupload.version>
		<gson.version>2.8.0</gson.version>
		<jackson.version>2.8.4</jackson.version>
		<ognl.version>3.1.12</ognl.version>
		<jstl.version>1.2</jstl.version>
	</properties>
	
  <dependencies>
    <!-- Spring Boot test -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
		</dependency>
		
		<!-- Spring Boot web -->
		<dependency>
		    <groupId>org.springframework.boot</groupId>
		    <artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<!-- Spring Boot devtools -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
			<optional>true</optional>
		</dependency>
		
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
		</dependency>
		
		<!-- jpa -->
		<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
		
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
		</dependency>

		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
		</dependency>
		<!-- Druid -->
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>druid</artifactId>
			<version>${druid.version}</version>
		</dependency>
		
		<!-- webservice支持 -->
		 <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-spring-boot-starter-jaxws</artifactId>
            <version>${cxf.version}</version>
        </dependency>
        <!-- 访问HTML -->
        <!-- <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency> -->
		
  </dependencies>
  
  <build>
    <finalName>project-name</finalName>
    	<plugins>
			<!-- Package as an executable jar -->
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<configuration>
					<failOnMissingWebXml>false</failOnMissingWebXml>  
					<mainClass>com.ideal.endtoend.Application</mainClass>
				</configuration>
			</plugin>
		
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<configuration>
					<source>${java.version}</source>
					<target>${java.version}</target>
				</configuration>
			</plugin>
		</plugins>
		<resources>
			<resource>
				<!-- rsl是否替换资源中的属性rsl-->
				<filtering>true</filtering>
				<directory>${basedir}\src\main\resources</directory>
			</resource>
		</resources>
  </build>
  
  
</project>

这里特别说下,高版本的SpringBoot一定要用高版本的Cxf,如 1.34的spring boot 用 3.17的CXF,我就是在这个上面吃了亏,用1.43的springBoot和3.17的CXF果断报错。


下面开始正题,首页写个接口

@WebService(name = "OrderWebService", targetNamespace = "http://www.namespace.com/")  
public interface OrderWebService {

	@WebMethod
	public String order(String requestXml);
}

然后写个类实现这个接口:

package com.ideal.endtoend.webservice.impl;

import java.io.StringWriter;
import java.util.Date;

import javax.annotation.Resource;
import javax.jws.WebService;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.ideal.endtoend.common.Contract;
import com.ideal.endtoend.configuration.URLProperties;
import com.ideal.endtoend.entity.DealRecord;
import com.ideal.endtoend.entity.WSBacklog;
import com.ideal.endtoend.entity.WorkerOrder;
import com.ideal.endtoend.entity.WorkerOrderResp;
import com.ideal.endtoend.service.DealRecordService;
import com.ideal.endtoend.service.WorkerOrderService;
import com.ideal.endtoend.utils.DateUtils;
import com.ideal.endtoend.utils.JacksonUtil;
import com.ideal.endtoend.webservice.OrderWebService;
import com.ideal.endtoend.webservice.utils.OAWebServiceUtil;

@Component
@WebService(targetNamespace="http://www.namespace.com/",serviceName="OrderWebService",
endpointInterface = "com.ideal.endtoend.webservice.OrderWebService")
public class OrderWebServiceImpl implements OrderWebService{

	private Logger logger = Logger.getLogger(getClass());
	
	
	@Override
	public String order(String requestXml) {
		
	}

}


这里说明下:
endpointInterface 
为接口路径

serviceName
和接口中的name一致


接下来是最后一步:

@Configuration
public class WebServiceConfig {
    
        
        @Bean(name = Bus.DEFAULT_BUS_ID)
        public SpringBus springBus() {
            return new SpringBus();
        }
        
        @Bean
        public OrderWebService orderWebService() {
            return new OrderWebServiceImpl();
        }
        
        @Bean
        public Endpoint endpoint() {
            EndpointImpl endpoint = new EndpointImpl(springBus(), orderWebService());
            endpoint.publish("/orderWebService");
            return endpoint;
        }
        
   

}
好了,完毕!
endpoint.publish("/orderWebService");
这个orderWebService就是你要调用的前置webService路径

访问下:http://localhost:8080/WebService/orderWebService/order?wsdl  看看有没有效果?

如果没问题那么可以往下走了。


启动项目,调用你的web service

 JaxWsDynamicClientFactory dcf =JaxWsDynamicClientFactory.newInstance();
	        org.apache.cxf.endpoint.Client client =dcf.createClient("http://localhost:8080/WebService/orderWebService/order?wsdl");
	        String param = "参数";
	        Object[] objects=client.invoke("order",param);
	        //输出调用结果
	        System.out.println("=============================="+objects[0].toString());

其他webservice接口也可以这么调用,不得不说springBoot的出现使得很多框架的使用都方便了很多。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值