SpringBoot + CXF 开发Web Service 服务
来源:互联网 发布:英国日常生活 知乎 编辑:程序博客网 时间:2024/06/14 19:15
首先是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 {@WebMethodpublic 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());@Overridepublic String order(String requestXml) {}}
endpointInterface为接口路径
serviceName和接口中的name一致
接下来是最后一步:
@Configurationpublic 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的出现使得很多框架的使用都方便了很多。
阅读全文
0 0
- SpringBoot + CXF 开发Web Service 服务
- 使用CXF开发web service服务中报错
- Web Service ---- CXF + SPRING 开发 SOAP服务
- Eclipse + Jboss + CXF构建Web Service服务
- Web Service 那点事儿 —— 使用 CXF 开发 SOAP 服务
- Web Service那些事 2/4 - 使用 CXF 开发 SOAP 服务
- Web Service 那点事儿(2)—— 使用 CXF 开发 SOAP 服务
- Web Service 那点事儿(2)—— 使用 CXF 开发 SOAP 服务
- Web Service 那点事儿(2)—— 使用 CXF 开发 SOAP 服务
- Web Service 那点事儿(4)—— 使用 CXF 开发 REST 服务
- Web Service 那点事儿(4)—— 使用 CXF 开发 REST 服务
- Web Service 那点事儿(2)—— 使用 CXF 开发 SOAP 服务
- 用cxf+spring开发web service
- CXF开发RESTful风格的Web Service
- Web Service -- 使用CXF开发入门
- Java使用Apache CXF开发Web Service
- Spring boot 整合CXF开发web service
- 使用Apache CXF开发Web Service步骤
- Git的使用--如何将本地项目上传到Github(两种简单、方便的方法)
- django Warning: (3135, "'NO_ZERO_DATE', 'NO_ZERO_IN_DATE' and 'ERROR_FOR_DIVISION_BY_ZERO' sql modes
- SQLSERVER DEMO
- maven(3)maven3.3.9使用入门
- PAT 乙级 1025
- SpringBoot + CXF 开发Web Service 服务
- delphi 进度条
- 深度学习系列讲座:谷歌翻译核心技术 Seq2Seq
- git checkout 和 git reset
- POJ 2923 Relocation(状压DP+01背包)
- android输入框取消横屏全屏输入
- HashMap与Hashtable的区别
- Android四大组件:BroadcastReceiver
- LMS权值更新法则(Least mean squares,最小均方)