基于web的cxf的第二种发布方式在搭建环境上和第一种方式基本相同,详细可参考《基于web的cxf的第一种发布方式——简单发布》
1、IHiService
- package com.njupt.cxf;
- import javax.jws.WebService;
- import javax.xml.ws.BindingType;
- import javax.xml.ws.soap.SOAPBinding;
- @WebService
- @BindingType(value=SOAPBinding.SOAP12HTTP_BINDING)
- public interface IHiService {
- public String sayHi(String name);
- }
package com.njupt.cxf;
import javax.jws.WebService;
import javax.xml.ws.BindingType;
import javax.xml.ws.soap.SOAPBinding;
@WebService
@BindingType(value=SOAPBinding.SOAP12HTTP_BINDING)
public interface IHiService {
public String sayHi(String name);
}
2、HiServiceImpl
- package com.njupt.cxf;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- public class IHiServieImpl implements IHiService {
- public String sayHi(String name) {
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd hh:mm:dd");
- return sdf.format(new Date()) + " hi " + name;
- }
- }
package com.njupt.cxf;
import java.text.SimpleDateFormat;
import java.util.Date;
public class IHiServieImpl implements IHiService {
public String sayHi(String name) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd hh:mm:dd");
return sdf.format(new Date()) + " hi " + name;
}
}
3、cxf-servlet.xml
在cxf-servlet.xml加上以下代码:
- <jaxws:server id="hiService" serviceClass="com.njupt.cxf.IHiService" address="/hi">
- <jaxws:serviceBean >
- <bean class="com.njupt.cxf.IHiServieImpl"></bean>
- </jaxws:serviceBean>
- <!-- 加入请求的消息拦截器 -->
- <jaxws:inInterceptors>
- <!-- 如果名字太长记不住的话,可以通过新建一个类,然后继承的方式来找到类的全名 -->
- <bean class="org.apache.cxf.interceptor.LoggingInInterceptor"></bean>
- </jaxws:inInterceptors>
- <!-- 加入响应的消息拦截器 -->
- <jaxws:outInterceptors>
- <bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"></bean>
- </jaxws:outInterceptors>
- </jaxws:server>
<jaxws:server id="hiService" serviceClass="com.njupt.cxf.IHiService" address="/hi">
<jaxws:serviceBean >
<bean class="com.njupt.cxf.IHiServieImpl"></bean>
</jaxws:serviceBean>
<!-- 加入请求的消息拦截器 -->
<jaxws:inInterceptors>
<!-- 如果名字太长记不住的话,可以通过新建一个类,然后继承的方式来找到类的全名 -->
<bean class="org.apache.cxf.interceptor.LoggingInInterceptor"></bean>
</jaxws:inInterceptors>
<!-- 加入响应的消息拦截器 -->
<jaxws:outInterceptors>
<bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"></bean>
</jaxws:outInterceptors>
</jaxws:server>
=========================================================
到这里基于web的cxf的第二种发布方式就算完成了。
但存在这样一个问题:每一次启动web项目时,第一次访问http://localhost:8080/CXF_3/cxf/hi?wsdl总是很慢,
原因:因为cxf-servlet,xml是在第一次访问http://localhost:8080/CXF_3/cxf/hi?wsdl时读取的,所以,第一次
访问http://localhost:8080/CXF_3/cxf/hi?wsdl时会显得比较慢。我们
解决方法:我们可以将读取cxf-servlet.xml环节放到web项目启动的时候,这时会增加web项目的启动时间,但是用户访问时的等待时间
就会降低。
1、在web.xml中加上以下代码
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath:cxf-servlet.xml</param-value>
- </context-param>
- <listener>
- <listener-class>
- org.springframework.web.context.ContextLoaderListener
- </listener-class>
- </listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:cxf-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
同时注释掉<init-param>如下所示:
- <servlet>
- <servlet-name>cxf</servlet-name>
- <!-- 如果名字太长记不住的话,可以通过新建一个类,然后继承的方式来获得类全名 -->
- <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
- <!--
- <init-param>
- <param-name>config-location</param-name>
- <param-value>classpath:cxf-servlet.xml</param-value>
- </init-param>
- -->
- </servlet>