加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

Webservice系列CXF方式(一)

发布时间:2020-12-17 00:27:45 所属栏目:安全 来源:网络整理
导读:? 一: CXF 中 maven 配置 !-- 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 - f

?

一: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>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读