`
schy_hqh
  • 浏览: 555870 次
  • 性别: Icon_minigender_1
社区版块
存档分类
最新评论

(一)CXF初步

 
阅读更多

CXF基于JAX-WS进行了功能的扩展,提供了更加方便的API进行webservice的开发

了解JAX-WS的基本知识对学习CXF很有帮助,尤其是SOAP协议中消息如何传递得比较清楚才行

 

学习CXF提供的第一个直观的好处:方便输出客户端与服务端交互的SOAP消息

使用Interceptor实现消息的输出,这样便于查看具体消息是如何传递的,也便于排查问题

 

服务端:

POM.XML

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.hqh.ws.cxf</groupId>
	<artifactId>cxf-first</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>cxf-first</name>
	<url>http://maven.apache.org</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<cxf.version>2.6.0</cxf.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.10</version>
			<scope>test</scope>
		</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-transports-http</artifactId>
			<version>${cxf.version}</version>
		</dependency>
		<!-- Jetty is needed if you're are not using the CXFServlet -->
		<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-rt-transports-http-jetty</artifactId>
			<version>${cxf.version}</version>
		</dependency>
	</dependencies>

</project>

 

接口

package com.hqh.ws.cxf;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;

@WebService
public interface IMyService {

	@WebMethod
	@WebResult(name="sayResult")
	public String sayHello(@WebParam(name="name") String name,
			@WebParam(name="license", header=true) String license);
}

 实现类

package com.hqh.ws.cxf;

import javax.jws.WebMethod;
import javax.jws.WebService;

@WebService(endpointInterface="com.hqh.ws.cxf.IMyService")
public class MyServiceImpl implements IMyService {

	@Override
	@WebMethod
	public String sayHello(String name,String license) {
		return (license==null||"".equals(license.trim())) ? "头信息为空" : "你好:"+name ;
	}

}

 

启动服务

package com.hqh.ws.cxf;

import javax.xml.ws.Endpoint;

import org.apache.cxf.endpoint.Server;
import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxws.JaxWsServerFactoryBean;

public class MyServer {

	public static void main(String[] args) {
		//startServer();
		startServerByCXF(); 
	}
	/**
	 * 基于JAX-WS的方式发布服务
	 * @param args
	 */
	public static void startServer() {
		Endpoint.publish("http://localhost:8888/cxf/ws", new MyServiceImpl());
	}
	
	/**
	 * 使用CXF发布服务
	 */
	public static void startServerByCXF() {
		JaxWsServerFactoryBean svrFactory = new JaxWsServerFactoryBean();
		svrFactory.setServiceClass(IMyService.class);
		svrFactory.setAddress("http://localhost:8888/cxf/ws");
		svrFactory.setServiceBean(new MyServiceImpl());
		//打印发出的消息
		svrFactory.getInInterceptors().add(new LoggingInInterceptor());
		//打印进入的消息
		svrFactory.getOutInterceptors().add(new LoggingOutInterceptor());
		svrFactory.create();
	}
}

 

 

客户端:

POM.XML

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.hqh.ws.cxf</groupId>
  <artifactId>cxf-client</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>cxf-client</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <cxf.version>2.6.0</cxf.version>
  </properties>

	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.10</version>
			<scope>test</scope>
		</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-transports-http</artifactId>
			<version>${cxf.version}</version>
		</dependency>
	</dependencies>
  
  	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.cxf</groupId>
				<artifactId>cxf-codegen-plugin</artifactId>
				<version>${cxf.version}</version>
				<executions>
					<execution>
						<id>generate-sources</id>
						<phase>generate-sources</phase>
						<configuration>
							<sourceRoot>${project.build.directory}/generated/cxf</sourceRoot>
							<wsdlOptions>
								<wsdlOption>
									<wsdl>http://localhost:8888/cxf/ws?wsdl</wsdl>
								</wsdlOption>
							</wsdlOptions>
						</configuration>
						<goals>
							<goal>wsdl2java</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>
</project>

 

测试类

package com.hqh.ws.cxf.cxf_client;

import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.junit.Test;

import com.hqh.ws.cxf.IMyService;
import com.hqh.ws.cxf.SayHello;

public class TestCXF {
	@Test
	public void test01() {
		JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
		factory.setServiceClass(IMyService.class);
		factory.setAddress("http://localhost:8888/cxf/ws?wsdl");
		//打印发出的消息
		factory.getInInterceptors().add(new LoggingInInterceptor());
		//打印进入的消息
		factory.getOutInterceptors().add(new LoggingOutInterceptor());
		IMyService service = (IMyService)factory.create();
		
		//代码优先将参数转换为一个对象去了
		SayHello say = new SayHello();
		say.setName("张三疯");
		//头信息
		String license = "this is license info";
		//调用服务
		String reply = service.sayHello(say,license).getSayResult();
		
		System.out.println(reply);
		System.exit(0);
	}
}

 

客户端看到的结果:

---------------------------
ID: 1
Address: http://localhost:8888/cxf/ws?wsdl
Encoding: UTF-8
Content-Type: text/xml
Headers: {Accept=[*/*], SOAPAction=[""]}
Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
	<soap:Header>
		<ns2:license xmlns:ns2="http://cxf.ws.hqh.com/">this is license info</ns2:license>
	</soap:Header>
	<soap:Body>
		<ns2:sayHello xmlns:ns2="http://cxf.ws.hqh.com/">
			<name>张三疯</name>
		</ns2:sayHello>
	</soap:Body>
</soap:Envelope>
--------------------------------------
2013-8-15 21:47:59 org.apache.cxf.services.IMyServiceService.IMyServicePort.IMyService
信息: Inbound Message
----------------------------
ID: 1
Response-Code: 200
Encoding: UTF-8
Content-Type: text/xml;charset=UTF-8
Headers: {Content-Length=[230], content-type=[text/xml;charset=UTF-8], Server=[Jetty(7.5.4.v20111024)]}
Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
	<soap:Body>
		<ns2:sayHelloResponse xmlns:ns2="http://cxf.ws.hqh.com/">
			<sayResult>你好:张三疯</sayResult>
		</ns2:sayHelloResponse>
	</soap:Body>
</soap:Envelope>
--------------------------------------
你好:张三疯

 

 

分享到:
评论
Global site tag (gtag.js) - Google Analytics