如何发布webservice

如题所述

1. CXF方式
CXF与spring搭建webservice是目前最流行的方式,但是传闻cxf与jdk1.5有些不兼容,我没有遇到过,我遇到的问题是cxf与was6.1.1不兼容,表现在cxf必须的jar包“wsdl4j-1.6.2.jar”报错,报的错为: java.lang.IncompatibleClassChangeError,明显的jar包不兼容问题,很是头痛,后来查找资料找到解决办法是,将上述jar包新建一个was共享库,可以解决,但是客户周经理不想用此种方式,因为需要修改was,于是改用了axis2方式,下文会介绍。该问题在此处做个记录,以后使用cxf与was的时候需要注意!!!

使用cxf+spring搭建WebService:
第一步,添加jar包。此处需要注意,不同环境(tomcat、was)jar也不一定相同,例如我本地cxf+spring只需要如下jar包:

而泰康的was环境则需要如下jar包:

明显的多了很多,原因应该是服务器jar包池的不同。根据错误提示缺什么补什么就可以了,注意jar包勿重复。
第二步,配置web.xml文件,如下(重要的地方已标记):

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/applicationContext.xml</param-value>
</context-param>
<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Character Encoding filter -->
<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>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<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>/webservice/*</url-pattern>
</servlet-mapping>

上述标记的地方,第一处是spring配置文件路径;第二出是wsdl地址内容;
第三步,编写接口类与实现类,注意注解

接口类
@WebService
public interface SendService {
public boolean sendOA(@WebParam(name="param")String param);
public boolean sendOrg(OrgEntity org);
}
实现类
@WebService(endpointInterface="com.service.SendService",serviceName="sendService")
public class SendServiceImpl implements SendService{
public boolean sendOA(String param) {
System.out.println("-------sendOA---------param:"+param);
if(param.equals("zhoujian")){
return true;
}
return false;
}
public boolean sendOrg(OrgEntity org) {
System.out.println("-------sendOrg--begin-------");
return true;
}
}

第四步,Spring配置文件

<?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: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.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="sendServie" implementor="com.service.impl.SendServiceImpl"
address="/sendServie" />

<!-- <jaxws:client id="sendServiceClient" serviceClass="com.service.SendService"
address="http://10.137.138.11:9080/Wb/webservice/sendServie?wsdl" />-->

</beans>

“jaxws:client”该标签可以不必写,访问时可以手动拼接该url
第五步,发布,直接部署到服务器,访问:

http://10.137.138.11:9080/Wb/webservice/sendServie?wsdl
温馨提示:答案为网友推荐,仅供参考
相似回答