开发Webservice接口可以使用Axis2,也可以使用CXF。可以参考下面几篇博客(主要讲述Axis2开发Webservice接口):
在Eclipse中使用Axis2插件生成Web Service服务端/客户端
Axis2与Web项目整合及Axis2在Web项目中整合Spring
利用AXIS2开发Webservice接口,浏览器访问返回纯JSON数据,没有xml标识
下面介绍Maven+SpringMVC+CXF实现Webservice接口。
第一步:
众所周知Maven管理了所有的JAR包,所以首先在pom.xml文件中引入Jar包。(这里遇到了一个坑,切记spring如果用的是4.0以上的版本,那么cxf就不能低于3.0,否则会报错)
POM.XML中需要增加如下配置:
<!-- 集成CXF实现Webservice接口 -->
<dependency>
<groupId>wsdl4j</groupId>
<artifactId>wsdl4j</artifactId>
<version>1.6.2</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http-jetty</artifactId>
<version>3.2.0</version>
</dependency>
第二步:创建Webservice接口类
CxyJkfwService.java
package com.inspur.tax.yjglpt.webserviceKfptService.service;
import javax.jws.WebService;
/**
*
* @Description: webservice服务
* @author fuxs
* @date 2016-2-2 下午2:46:33
*
*/
@WebService
public interface CxyJkfwService {
public String swapInfo(String requestXml) throws Exception;
}
CxyJkfwServiceImpl.java实现CxyJkfwService接口继承SpringBeanAutowiringSupport
package com.inspur.tax.yjglpt.webserviceKfptService.service;
import javax.jws.WebService;
import org.springframework.web.context.support.SpringBeanAutowiringSupport;
@WebService(endpointInterface = "com.inspur.tax.yjglpt.webserviceKfptService.service.CxyJkfwService",serviceName="cxyJkfwService")
public class CxyJkfwServiceImpl extends SpringBeanAutowiringSupport implements CxyJkfwService{
@Override
public String swapInfo(String requestXml) throws Exception {
String xml = "我爱你中华";
return xml;
}
}
第三步:创建一个普通的xml文件,用于定义cxf服务端的配置
名称你自己随便写,我这里叫:cxf-servlet.xml
(这里注释了此段代码,cxf3.0以上不需要此xml文件了,否则会有警告<!-- 新版本说不需要此xml文件 <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" /> -->)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:http-conf="http://cxf.apache.org/transports/http/configuration"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd
http://cxf.apache.org/transports/http/configuration
http://cxf.apache.org/schemas/configuration/http-conf.xsd">
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<!-- 新版本说不需要此xml文件 <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" /> -->
<!-- Start 供WebService调用的业务Service -->
<bean id="test12333WS" class="com.inspur.tax.yjglpt.webserviceKfptService.service.CxyJkfwServiceImpl"> </bean>
<jaxws:server id="cxyJkfwService" address="/cxyJkfwService">
<jaxws:serviceBean>
<ref bean="test12333WS"/>
</jaxws:serviceBean>
</jaxws:server>
<!-- cui_xyang 解析说明:class: 这个是你的webservice的接口实现类:包名.类名
address: 这个名字是对外的接口路径名称,你自己怎么开心怎么来
jaxws中id:这个名字对应实现类中的servicename注解 -->
<!-- End 供WebService调用的业务Service-->
<!-- Start End 客户端使用
<bean id="client"
class="com.huating.outService.Test12333WS"
factory-bean="clientFactory"
factory-method="create"/>
<bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
<property name="serviceClass" value="com.huating.outService.Test12333WS"/>
<property name="address" value="http://localhost:8080/yzxx/services/test12333WS"/>
</bean> -->
</beans>
第四步:web.xml加载cxf-servlet.xml,配置CXF
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<display-name>manager</display-name>
<!-- 为了cxf而加入 start -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:cxf-servlet.xml
</param-value>
</context-param>
<!-- Spring监听配置,Web容器启动自动装配ApplicationContext的配置信息-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 为了cxf而加入 end -->
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<!-- springmvc配置 -->
<servlet>
<servlet-name>springweb</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springweb</servlet-name>
<url-pattern>*.shtml</url-pattern>
</servlet-mapping>
<!--webservice服务端 cxy-->
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/services/yshdjkptApi/*</url-pattern>
</servlet-mapping>
<!-- Spring字符编码过滤器配置,处理中文乱码 -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>*.shtml</url-pattern>
</filter-mapping>
<error-page>
<error-code>404</error-code>
<location>/404.shtml</location>
</error-page>
<welcome-file-list>
<welcome-file>/login.shtml</welcome-file>
</welcome-file-list>
</web-app>
测试WebService接口
最后请各位关注一下公众号,会不定时推送一些开发中碰到的问题的解决方法,以及会分享一些开发视频。资料等。公众号里有联系方式,大家可以一起交流分享开发中遇到的问题,以及总结的经验,请大家关注一下谢谢: