由于之前项目用到了cxf webservice接口开发,今天特意写一下个人的总结,诉说遇到的坑,分两种方式搭建环境,一种是整合spring,另一种是脱离spring框架进行独立开发,详细请继续浏览,,,,,啦啦啦啦啦啦啦,不好的地方希望大家指出。
一、整合spring框架进行webservice搭建
1、在spring包存在的情况下在pom.xml配置cxf以下代码
<!-- spring cxf start -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf</artifactId>
<version>3.1.5</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.1.5</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.1.5</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-core</artifactId>
<version>3.1.5</version>
</dependency>
<dependency>
<groupId>org.apache.neethi</groupId>
<artifactId>neethi</artifactId>
<version>3.0.2</version>
</dependency>
<dependency>
<groupId>org.apache.ws.xmlschema</groupId>
<artifactId>xmlschema-core</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity</artifactId>
<version>1.7</version>
</dependency>
<dependency>
<groupId>org.codehaus.woodstox</groupId>
<artifactId>woodstox-core-asl</artifactId>
<version>4.4.1</version>
</dependency>
<dependency>
<groupId>org.apache.xmlbeans</groupId>
<artifactId>xmlbeans</artifactId>
<version>2.3.0</version>
</dependency>
<!-- spring cxf end -->
配置好了之后最好先项目右键-->Maven-->Update Maven Project,将要下载的jar包下载下来才进行开发。
2、新建一个接口及其它的实现类如下
接口:
package com.lq.service.interfaces.ws;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
/**
* webservice服务测试
*
* @author lq 2017年12月20日14:54:13
*/
@WebService
public interface IHelloWorldService {
/**
* 测试webservice
*
* @param inParam
* @return
* @throws Exception
*/
@WebResult(name = "returnStr")
public String sayHello(@WebParam(name = "inParam") String inParam)
throws Exception;
}
实现类:
package com.lq.service.impl.ws;
import javax.jws.WebService;
import com.lq.service.interfaces.ws.IHelloWorldService;
/**
* webservice
* @author Administrator
*
*/
@WebService(endpointInterface="com.lq.service.interfaces.ws.IHelloWorldService") //注意这个地址,是接口的地址,
public class HelloWorldService implements IHelloWorldService {
@Override
public String sayHello(String inParam) throws Exception {
// TODO Auto-generated method stub
String str = "say HelloWorld";
System.out.println(str);
return str;
}
}
3、新建一个spring-cxf.xml的配置文件,进行配置发布webservice,如下
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.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-servlet.xml" />
<!-- spring 3.0后貌似不再需要 -->
<!-- <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" /> -->
<!-- 配置发布服务 -->
<!-- IHelloWorldService webservice接口 (可以发布多个地址)-->
<jaxws:server id="helloWorld" address="/helloWorld"
serviceClass="com.lq.service.interfaces.ws.IHelloWorldService">
<jaxws:serviceBean>
<!-- 引用容器中已经配置好的bean 接口的实现类-->
<bean class="com.lq.service.impl.ws.HelloWorldService" />
</jaxws:serviceBean>
</jaxws:server>
</beans>
4、在application-spring.xml加载地方同样加载配置spring-cxf.xml文件
5、配置web.xml,如下
<!-- 1、Spring整合 webservice服务端配置 -->
<servlet>
<servlet-name>WebServiceServlet</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>WebServiceServlet</servlet-name>
<url-pattern>/webservice/*</url-pattern>
</servlet-mapping>
6、启动项目,访问:http://localhost:521/mymaven/webservice/helloWorld?wsdl,如果出现如下,则发布部署成功
二、脱离spring框架进行webservice搭建
这个过程和(一、整合spring框架进行webservice搭建)的步骤1、2相同,我以下从步骤三开始讲述,
3、写一个类, 继承CXFNonSpringServlet,重写loadBus方法
public class MyCXFSpringServlet extends CXFNonSpringServlet {
private static final long serialVersionUID = -595850764947714199L;
public void loadBus(ServletConfig servletConfig) throws ServletException {
// 内部需要从环境变量中取得org.apache.cxf.bus.factory的值,如果为空就默认按照spring的方式加载。于是在 //类的最开始加入一行代码
// System.setProperty("org.apache.cxf.bus.factory", "org.apache.cxf.bus.CXFBusFactory");
super.loadBus(servletConfig);
Bus bus = this.getBus();
BusFactory.setDefaultBus(bus);
Enumeration<String> enums = getInitParameterNames();
while (enums.hasMoreElements()) {
String key = enums.nextElement();
String value = getInitParameter(key);
try {
Class clz = Class.forName(value);
try {
Endpoint.publish(key, clz.newInstance());
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
}
4、配置web.xml
<!-- 2、 webservice脱离spring配置 可以配置多个服务,也就是多个接口的实现类-->
<!-- <servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>
<!--MyCXFNonSpringServlet 路径 -->
</servlet-class>
<init-param>
<param-name>/helloWorld</param-name>
<param-value>com.lq.service.impl.ws.HelloWorldService</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/webservice/*</url-pattern>
</servlet-mapping> -->
5、启动项目,访问:http://localhost:521/mymaven/webservice/helloWorld?wsdl,如果出现和上面一样,则发布部署成功
三、总结
webservice接口开发以后可能还是会有用处,由于之前在项目当中遇到项目版本特别低,sping2.1,最后就想出了通过脱离spring进行开发,觉得有用的小伙伴们可以学习一下