Spring integrates Apache Cxf to implement WebService

The server-side example is found on the Internet, and the Spring version and CXF version are modified.

1. POM configuration

The project is managed by maven, and the pom configures the libraries used.

<properties>

        <!-- spring version number-->

        <spring.version>4.0.6.RELEASE</spring.version>

        <cxf.version>3.1.8</cxf.version>

    </properties>

<dependencies>

<dependency>

<groupId>junit</groupId>

<artifactId>junit</artifactId>

<version>3.8.1</version>

<scope>test</scope>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-core</artifactId>

<version>${spring.version}</version>

</dependency>

 

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-context</artifactId>

<version>${spring.version}</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-web</artifactId>

<version>${spring.version}</version>

</dependency>

 

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-webmvc</artifactId>

<version>${spring.version}</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-aop</artifactId>

<version>${spring.version}</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-jdbc</artifactId>

<version>${spring.version}</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-context-support</artifactId>

<version>${spring.version}</version>

</dependency>

<dependency>

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

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

<version>${cxf.version}</version>

</dependency>

<dependency>

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

<artifactId>cxf-rt-bindings-soap</artifactId>

<version>${cxf.version}</version>

</dependency>

<dependency>

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

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

<version>${cxf.version}</version>

</dependency>

<dependency>

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

<artifactId>cxf-rt-ws-security</artifactId>

<version>${cxf.version}</version>

</dependency>

<dependency>

    <groupId>org.codehaus.woodstox</groupId>

    <artifactId>stax2-api</artifactId>

    <version>3.1.1</version>

 

2、开发服务器端接口

复制代码
package com.moon.cxfWebservice.server;

import javax.jws.WebParam;
import javax.jws.WebService;

@WebService
public interface Greeting {
    public String greeting(@WebParam(name="username")String userName);
}
复制代码

然后开发实现类

复制代码
package com.moon.cxfWebservice.server;

import java.util.Calendar;

import javax.jws.WebService;

@WebService(endpointInterface="com.moon.cxfWebservice.server.Greeting")
public class GreetingImpl implements Greeting{

    public String greeting(String userName) {
        return "Hello " + userName + ", currentTime is "
                + Calendar.getInstance().getTime();
    }

}
复制代码

至此,服务端的代码就开发完成了。

3、在web.xml和spring配置文件中配置

首先,在web.xml中,需配置servlet,如下图配置:

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

  </servlet-mapping>

另外,还行配置spring相关的上下文监听,如:

context-param>

    <param-name>contextConfigLocation</param-name>

    <param-value>/WEB-INF/config/spring.xml</param-value>

  </context-param>

  <listener>

    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

  </listener>

  <filter>

    <filter-name>encodingFilter</filter-name>

    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>

    <init-param>

      <param-name>encoding</param-name>

      <param-value>UTF-8</param-value>

    </init-param>

    <init-param>

      <param-name>forceEncoding</param-name>

      <param-value>true</param-value>

    </init-param>

  </filter>

  <filter-mapping>

    <filter-name>encodingFilter</filter-name>

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

  </filter-mapping>

 

在web.xml中,声明的spring的配置文件是spring.xml,所以,在Spring.xml中,还需要配置webservice相关的服务。

<bean id="greetingImpl" class="com.moon.cxfWebservice.server.GreetingImpl"/>

 <jaxws:endpoint id="greeting"  implementor="#greetingImpl"   address="/Greeting" />

 

<bean id="hello" class="com.cigna.cmc.cxf.service.impl.HelloWorldImpl" />

<jaxws:endpoint id="helloWorld" implementor="#hello" address="/HelloWorld">

</jaxws:endpoint>

 

其中,address是访问的链接中对应该服务的标识。如访问helloworld的配置,访问的完整地址为:

http://localhost:9080/cxfWSServer/webservice/HelloWorld?wsdl

 

以上,服务器端的开发就完成了。

 

4、开发客户端

客户端的访问方式,通过查找资源,一共实现了三种,

第一种方式:

JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.setServiceClass(Greeting.class);
factory.setAddress("http://localhost:9080/cxfWSServer/webservice/Greeting?wsdl");

Greeting service = (Greeting) factory.create();

String name = service.greeting("白嘉轩先生");
System.out.println("********返回值="+name);

第二种访问方式,动态访问指定的方法:

String wsUrl = "http://localhost:9080/cxfWSServer/webservice/HelloWorld?wsdl";
DynamicClientFactory objDynamicClientFactory = JaxWsDynamicClientFactory.newInstance();
Client objClient = objDynamicClientFactory.createClient(wsUrl);

String method="sayHello";
Endpoint endpoint = objClient.getEndpoint();
QName opName = new QName(endpoint.getService().getName().getNamespaceURI(), method);
BindingInfo bindingInfo = endpoint.getEndpointInfo().getBinding();
if (bindingInfo.getOperation(opName) == null) {
   for (BindingOperationInfo operationInfo : bindingInfo.getOperations()) {
      if (method.equals(operationInfo.getName().getLocalPart())) {
         opName = operationInfo.getName();
         break;
      }
   }
}
try{
   Object[] object = objClient.invoke(opName, "白嘉轩","F");
   for(int i=0;i<object.length;i++){
      System.out.println("*********object="+object[i].toString());
   }

}catch (Exception e){
   e.printStackTrace();
}

 

第三种访问方式:

该访问方式是将需要访问的服务配置在Spring项目的配置文件中,之后通过配置文件找到对应的服务。

在Spring的配置文件中配置:

<jaxws:client id="helloWorld" serviceClass="com.cmdi.ws.HelloWorld"
address="http://localhost:9080/cxfWSServer/webservice/HelloWorld?wsdl"/>

其中,id的值为在加载了Spring配置文件后,通过id找到对应的服务。

serviceClass为该服务的接口类,应该是在客户端有一个该接口类文件,在服务器端有一个相同的实现类文件。address为访问服务的地址,且“?wsdl”可以不加上,经验证均可以正确调用webservice。

 

 此外,对于客户端需要访问的服务器端的服务,客户端都需要存在该服务的接口类,同时,客户端的接口类需要指明该服务的命名空间。如

@WebService(targetNamespace = "http://impl.service.cxf.cmc.cigna.com/")
public interface HelloWorld {
@WebResult(name = "String")
String sayHello(@WebParam(name = "name") String name,
                @WebParam(name = "sex") String sex);
  void test();
}

 

 targetNamespace指定了命名空间,根据观察,该命名空间应该是:

http://+该服务类在服务器端的完整路径的倒叙。

com.cigna.cmc.cxf.service.impl.HelloWorld 

 

 

 

 

 

 

 

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327031681&siteId=291194637