微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐