Webservice系列CXF方式(一)

 

一:CXFmaven配置

<!-- cxf配置 -->

<dependency>

    <groupId>org.apache.cxf</groupId>

    <artifactId>cxf</artifactId>

    <version>2.6.2</version>

</dependency>

 

<dependency>

    <groupId>org.apache.cxf</groupId>

    <artifactId>cxf-rt-frontend-jaxws</artifactId>

    <version>2.6.2</version>

</dependency>

<dependency>

    <groupId>org.apache.cxf</groupId>

    <artifactId>cxf-rt-transports-common</artifactId>

    <version>2.5.6</version>

    <type>jar</type>

    <scope>compile</scope>

</dependency>

<dependency>

    <groupId>org.apache.cxf</groupId>

    <artifactId>cxf-rt-core</artifactId>

    <version>2.6.2</version>

    <type>jar</type>

    <scope>compile</scope>

</dependency>

<dependency>

    <groupId>org.apache.cxf</groupId>

    <artifactId>cxf-rt-transports-http-jetty</artifactId>

    <version>2.6.2</version>

    <type>jar</type>

    <scope>compile</scope>

</dependency>

 

二:spring.xml文件配置和web.xml文件配置

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

 xmlns:jaxws="http://cxf.apache.org/jaxws"

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" />

<import resource="classpath:META-INF/cxf/cxf-extension-xml.xml" />

 

<bean id="mapInfoServiceImpl" class="com.dingli.webservice.MapInfoServiceImpl"></bean>

<jaxws:endpoint id="mapInfoService" implementor="#mapInfoServiceImpl" address="http://localhost:8080/MapTest/mapInfoService">

 </jaxws:endpoint>

 

</beans>

 

web.xml配置

 

<servlet-mapping>

       <servlet-name>CXFService</servlet-name>

       <url-pattern>/ws/*</url-pattern>

    </servlet-mapping>

    <welcome-file-list>

        <welcome-file>gpsview.jsp</welcome-file>

    </welcome-file-list>

 

 

三:接口和实现类定义

@WebService

publicinterface IMapInfoService {

 

    @WebMethod(operationName = "getNearbySearchInfo")

    @WebResult(name = "result")

    public String getNearbySearchInfo(@WebParam(name = "lat") String lat,

           @WebParam(name = "lng") String lng);

 

@WebService(endpointInterface = "com.dingli.webservice.IMapInfoService")

publicclass MapInfoServiceImpl implements IMapInfoService {

 

 

四:发布

MapInfoService mapInfoService = new MapInfoService();

Endpoint.publish("http://localhost:8080/mapInfoService", mapInfoService);

System.out.println("Server is Start...");

 

五:访问

http://localhost:8080/MapTest/ws/mapInfoService?wsdl

 

 

六:struts2cxf webService组合问题,被拦截器阻拦

1:自定义拦截器,放开对ws的过滤:

publicclass ExtendStrutsFilter extends StrutsPrepareAndExecuteFilter {

 

    publicvoid doFilter(final ServletRequest req, final ServletResponse res,

           final FilterChain chain) throws IOException, ServletException {

       final HttpServletRequest request = (HttpServletRequest) req;

       System.out.println("rrrrrrrr=" + request.getRequestURI());

 

       // 不过来URL,可以自己添加

       if (request.getRequestURI().contains("/ws")) {

           chain.doFilter(req, res);

       } else {

           super.doFilter(request, res, chain);

       }

 

       // if ("/MapTest/ws/mapInfoService".endsWith(request

       // .getRequestURI())

       // || "/ws/".endsWith(request.getRequestURI())) {

       //

       // chain.doFilter(req, res);

       // }

    }

}

 

2web.xml配置

<filter>

    <filter-name>struts2</filter-name>

    <!-- filter-class>org.apache.struts2.dispatcher.FilterDispatcher </filter-class -->

    <filter-class>com.dingli.struts2.ExtendStrutsFilter</filter-class>

    <!-- filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class -->

</filter>

<filter-mapping>

    <filter-name>struts2</filter-name>

    <url-pattern>/*</url-pattern>

</filter-mapping>

 

<!-- 设置cxf接口设备 -->

<!-- 负责处理由JavaBeans Introspector的使用而引起的缓冲泄露 ,可以保证在web 应用关闭的时候释放与掉这个web 应用相关的class

       loader 和由它管理的类 -->

<listener>

    <listener-class>org.springframework.web.util.IntrospectorCleanupListener

    </listener-class>

</listener>

 

<servlet>

    <servlet-name>CXFService</servlet-name>

    <servlet-class>org.apache.cxf.transport.servlet.CXFServlet

    </servlet-class>

    <!-- load-on-startup>1</load-on-startup -->

</servlet>

 

<servlet-mapping>

    <servlet-name>CXFService</servlet-name>

    <url-pattern>/ws/*</url-pattern>

</servlet-mapping>

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

tony168hongweigan

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值