cxf的使用及安全校验-01创建简单的服务端接口

最近因为项目的需要,研究了一下webservice的使用;

这里以cxf2.7.0为例,大致介绍一下,也用于备份啦(张立胜)

大致介绍一下项目的情况:项目有maven管理,webservice调用的方式cxf(是通过接口地址在客户端先生成相关文件)

我们先来介绍服务端:

1.引入相关jar包,因为项目是通过maven管理的,所以直接在pom文件添加

<dependency>
<groupId>org.apache</groupId>
<artifactId>cxf</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>org.apache.neethi</groupId>
<artifactId>neethi</artifactId>
<version>3.0.2</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.2.2</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.2.1</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore-nio</artifactId>
<version>4.2</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpasyncclient</artifactId>
<version>4.0-beta3</version>
</dependency>
<dependency>
<groupId>org.apache.ws.xmlschema</groupId>
<artifactId>xmlschema-core</artifactId>
<version>2.0.3</version>
</dependency>
<dependency>
<groupId>org.apache.ws.security</groupId>
<artifactId>wss4j</artifactId>
<version>1.6.10</version>
</dependency>

2.创建webservice接口,例如:

package com.cpic.caf.demo.service;

import javax.jws.WebService;

@WebService
public interface GreetingService {
public String greeting(String username);
}

3.创建webservice接口的实现类

package com.cpic.caf.demo.service.impl;

import javax.jws.WebService;

import com.cpic.caf.demo.service.GreetingService;

@WebService(endpointInterface="com.cpic.caf.demo.service.GreetingService")
public class GreetingServiceImpl implements GreetingService {
@Override
public String greeting(String username) {
return "Hello ! " + username;
}

}

4.配置applicationContext.xml文件

在xml文件中配置我们要发布的webservice接口,

<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="greetingService"
implementor="com.cpic.caf.demo.service.impl.GreetingServiceImpl"
address="/greetingService" >
</jaxws:endpoint>

注:在此xml文件头中要添加上 xmlns:jaxws="http://cxf.apache.org/jaxws",xsi:schemaLocation后面要加上http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd ,不然启动的时候会报<jaxws:endpoint>无法识别的错误

5.配置web.xml

在web.xml中添加

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext.xml</param-value>
</context-param>

<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><!--这样配置后,本例的接口地址则为 localhost:8080(ip地址)/项目名/webservice/greetingService-->
</servlet-mapping>

现在启动你的项目,看是否可以正常访问你的接口地址localhost:8080(ip地址)/项目名/webservice/greetingService?wsdl

posted @ 2013-10-29 22:57  星空雪苑  阅读(988)  评论(0编辑  收藏  举报