前言
公司需要本系统对外提供webservice接口,供其他系统上传数据到本系统,该项目的框架用的是SSH,自己开发学习的过程中也写了一个使用springboot的demo。
SSH框架下开发Webservice接口
1.准备好依赖包
使用的依赖包过多,至少的依赖包(包含客户端和服务器端)需要的如下(本文章只涉及接口端开发):
1.cxf-2.1.3.jar
2.cxf-mainfest.jar
3.geronimo-annotation_1.0_spec_1.1.1.jar
4.geronimo-stax-api_1.0_spec-1.0.1.jar
5.geronimo-ws-metadata_2.0_spec-1.1.2.jar
6.jaxb-api-2.1.jar
7.jaxb-impl-2.1.7.jar
8.neethi-2.0.4.jar
9.wsdl4j-1.6.2.jar
10.wstx-asl-3.2.6.jar
cxf 的 jar下载地址: http://cxf.apache.org/download.html
我这里整理了这些jar存放在百度网盘中:
路径为:https://pan.baidu.com/s/11eOKBFFwV-t5zWZujC6A6g 提取码:qkir
2.创建接口和实现类
package com.text.business.report.service;
import javax.jws.WebService;
@WebService
public interface IImport_Abbrevlist_Service {
/**
* 导入数据的服务功能
* @return
*/
public abstract String HelloWorld(String text);
}
package com.text.business.report.service;
import javax.jws.WebService;
/*
*endpointInterface 指定的是实现类所implements的接口的具体路径
*serviceName 指定的是服务的名称
*/
@WebService(endpointInterface="com.text.business.report.service.IImport_Abbrevlist_Service",serviceName="import_Abbrevlist_Service")
public class Import_Abbrevlist_ServiceImpl implements IImport_Abbrevlist_Service {
@Override
public String HelloWorld(String text) {
return "helloworld,my name is "+text;
}
}
3.配置spring文件
配置spring文件可以生成个单独 的xml文件,也可以直接在原有的spring文件中添加,因为我这个项目的spring文件是有多少个实体类就有多少个spring 配置文件,我这边就直接原有的spring-config-abbrevlist.xml中直接加配置了。
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<jaxws:endpoint id="import_Abbrevlist_Service" implementor="com.pccw.business.report.service.Import_Abbrevlist_ServiceImpl" address="/import_Abbrevlist_Service">
</jaxws:endpoint>
</beans>
关于bean注入service和dao的代码块因为设计公司隐私,所以就删除了,添加配置分三块,1.添加xmlns:jaxws标签,添加http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd。2.导入三个resource,这三个位置在cxf.jar包中有,有兴趣的朋友可以看下源码。3.添加jaxws:endpoint标签。
关于jaxws:endpoint标签中id很通俗易懂就是唯一标识,implementor指的为接口实现类的具体路径,address为访问的最后路径。
4配置web.xml文件
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>
org.apache.cxf.transport.servlet.CXFServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/entry/services/*</url-pattern>
</servlet-mapping>
这里主要就是处理请求跳转,添加servlet。现在访问的路径就是http://localhost:8080/entry/services/import_Abbrevlist_Service?wsdl
5在浏览器上访问该路径
是可以访问的,然后在SoapUI上模拟传数据
发现都没有问题,这就完成了webservice接口端的开发了。
2 springboot框架开发webservice接口
1 添加依赖
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-spring-boot-starter-jaxws</artifactId>
<version>3.3.3</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.4.1.Final</version>
</dependency>
上面的依赖是cxf框架所需要的依赖,下面的依赖是因为我这边编写完成后报了一个javax.validation.ValidationException: Unable to create a Configuration, because no Bean Validation provider could be found. Add a provider like Hibernate Validator (RI) to your classpath.
这个bug,需要添加上hibernate-validator依赖,如果没有出现这个问题自然不需要添加。
2 编写接口和实现类
package com.example.service;
import javax.jws.WebParam;
import javax.jws.WebService;
@WebService(targetNamespace="http://service.example.com")
public interface HelloWorldService {
public abstract String hello(@WebParam(name="helloName") String helloName);
}
targetNamespace:http://后接该接口所在路径倒序
package com.example.service;
import javax.jws.WebService;
import org.springframework.stereotype.Component;
@Component
@WebService(targetNamespace="http://service.example.com",
endpointInterface ="com.example.service.HelloWorldService")
public class HelloWorldServiceImpl implements HelloWorldService{
@Override
public String hello(String text) {
return "HelloWorld my name is "+text;
}
}
targetNamespace与接口中的路径一样
endpointInterface 接口所在路径
3提供配置类,也叫发布该webservice
package com.example.action;
import org.apache.cxf.Bus;
import org.apache.cxf.bus.spring.SpringBus;
import javax.xml.ws.Endpoint;
import org.apache.cxf.jaxws.EndpointImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.example.service.HelloWorldServiceImpl;
@Configuration
public class WebServiceConfig {
@Autowired
private HelloWorldServiceImpl helloWord;//接口实现类
@Bean(name = Bus.DEFAULT_BUS_ID)
public SpringBus springBus() {
return new SpringBus();
}
@Bean
public Endpoint endpoint() {
EndpointImpl endpoint=new EndpointImpl(springBus(), helloWord);
endpoint.publish("/helloWorld");//访问地址
return endpoint;
}
}
springBus 为非必写项
endpoint为发布方法
其默认的路径为127.0.0.1:8080/services/
该demo的访问路径为http://127.0.0.1:8080/services/helloWorld?wsdl
4 页面测试和soapUI测试
这都是没问题的。
总结。
这大致上就是基本的webservice接口的开发了,若有问题欢迎指出,也欢迎一起探讨。