cxf jax-rs client 笔记。

本文介绍如何利用JAX-RS规范在Java中实现服务间调用,包括构建Service接口、创建Service实例及配置Spring环境,同时解决XML消息格式与类名不匹配的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

参考:http://cxf.apache.org/docs/jax-rs-client-api.html

第一步:

         根据WS服务端提供的信息 编写一个Service接口

例如:

import javax.ws.rs.POST;
import javax.ws.rs.Path;
import com.dotwconnect.us.xsd.getallcountries.Customer;
import com.dotwconnect.us.xsd.getallcountries_response.ResultType;
@Path("/gateway.dotw")
public interface CountriesService {

	@POST
	ResultType getAllCountries(Customer customer);
	
}

第二步:

    通过JAXRSClientFactory 工厂对象创建Service

CountriesService cs = JAXRSClientFactory.create("http://localhost:8888",
				CountriesService.class);
		
 Customer c = new Customer() ;	//请求参数

ResultType xxx = cs.getAllCountries(c);  //访问WS


注释:

   发送出去的XML消息,如果要去掉namespace 信息 ,可以将生成的客户端对象中的package-info对象中的@XmlSchema注解中的 namespace 属性删掉。

 

如果返回过来的xml消息。节点名称与我们类名不符合。

如:返回过来的消息为

   <result>…</result>

但是我们生成的类名为 ResultType

那么使用JAXb Unmarshaller 的时候,就有问题 。解决办法就是给ResultType加上一个@XmlRootElement(name="result")注解

 


Spring中配置

在项目中加入jaxrs-https.xml文件,内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:jaxrs="http://cxf.apache.org/jaxrs"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
           http://www.springframework.org/schema/tx
		   http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
		   http://www.springframework.org/schema/aop
		   http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-3.1.xsd
           http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd">
           
	<jaxrs:client id="countriesClient"  
		serviceClass="com.zf.service.CountriesService"
		address="http://localhost:8888"
		inheritHeaders="true">
		<jaxrs:headers>
			<entry key="Accept" value="text/xml" />
		</jaxrs:headers>
	</jaxrs:client>
	
</beans>

然后就可以通过下面的方式获取服务接口了

ApplicationContext cxt = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml","jaxrs-https.xml"});

		CountriesService countriesService = (CountriesService)cxt.getBean("countriesClient");


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值