cxf jax-rs spring client
2013-02-28 23:03
381 查看
参考:http://cxf.apache.org/docs/jax-rs-client-api.html
第一步:
根据WS服务端提供的信息 编写一个Service接口
例如:
[java]
view plaincopyprint?
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
[java]
view plaincopyprint?
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文件,内容如下:
[html]
view plaincopyprint?
<?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>
然后就可以通过下面的方式获取服务接口了
[java]
view plaincopyprint?
ApplicationContext cxt = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml","jaxrs-https.xml"});
CountriesService countriesService = (CountriesService)cxt.getBean("countriesClient");
第一步:
根据WS服务端提供的信息 编写一个Service接口
例如:
[java]
view plaincopyprint?
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);
}
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
[java]
view plaincopyprint?
CountriesService cs = JAXRSClientFactory.create("http://localhost:8888",
CountriesService.class);
Customer c = new Customer() ; //请求参数
ResultType xxx = cs.getAllCountries(c); //访问WS
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文件,内容如下:
[html]
view plaincopyprint?
<?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>
<?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>
然后就可以通过下面的方式获取服务接口了
[java]
view plaincopyprint?
ApplicationContext cxt = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml","jaxrs-https.xml"});
CountriesService countriesService = (CountriesService)cxt.getBean("countriesClient");
相关文章推荐
- 使用 Spring + CXF 发布 REST 服务(jax-RS)
- cxf jax-rs client 笔记
- CXF 开发RESTful WebService服务(Spring+JPA服务端,JAXRS2.0+WebClient客户端)
- WebService(注解),CXF框架(jax-ws,Jax-rs,与spring整合)
- 框架 day64 WebService(注解),CXF框架(jax-ws,Jax-rs,与spring整合)
- CXF系列之JAX-RS:CXF与spring集成发布REST服务
- 【Apache CXF】CXF对JAX-RS的支持 推荐
- 基于CXF的JAX-WS、JAX-RS(RESTful)的webService
- maven+springmvc错误 JAX-RS (REST Web Services) 2.0 can not be installed
- jax-ws spring client 详解
- 基于CXF的JAX-WS、JAX-RS(RESTful)的webService
- Spring Boot与JAX-RS框架Jersey的完美搭配
- CXF整合Spring之JaxWsProxyFactoryBean调用
- CXF系列之JAX-RS:CXF发布与调用REST服务
- Spring Boot 集成 resteasy篇 — jax-rs常用注解介绍
- maven+springmvc错误 JAX-RS (REST Web Services) 2.0 can not be installed
- CXFRS和Spring整合服务器端配置
- WebService之JAX-WS、CXF、Spring3.0+
- Unable to locate Spring NamespaceHandler for XML schema namespace [http://cxf.apache.org/jaxws]
- Unable to locate Spring NamespaceHandler for XML schema namespace [http://cxf.apache.org/jaxws]