Spring整合CXF,发布RSETful 风格WebService--编写客户端代码,调用RESTful WebSer

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

               
package com.hoo.client;
 
import java.io.IOException;
import javax.ws.rs.core.MediaType;
import org.apache.cxf.jaxrs.client.WebClient;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.hoo.entity.MapBean;
import com.hoo.entity.User;
import com.hoo.entity.Users;
import com.hoo.service.RESTSample;
 
/**
 * <b>function:</b> RESTful风格WebService
 * @author hoojo
 * @createDate 2012-7-20 下午03:31:03
 * @file RSETServiceClient.java
 * @package com.hoo.client
 * @project CXFWebService
 * @blog http://blog.csdn.net/IBM_hoojo
 * @email [email protected]
 * @version 1.0
 */
public class RSETServiceClient {
 
    private static WebClient client;
    
    @Before
    public void init() {
        // 手动创建webClient对象,注意这里的地址是发布的那个/rest地址
        //String url = "http://localhost:8000/CXFWebService/rest/";
        //client = WebClient.create(url);
 
        // 从Spring Ioc容器中拿webClient对象
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext-client.xml");
        client = ctx.getBean("webClient", WebClient.class);
    }
    
    @After
    public void destory(){
    }
    
    @Test
    public void testGet() {
        System.out.println(client.path("sample").accept(MediaType.TEXT_PLAIN).get(String.class));
    }
    
    @Test
    public void testRequest() {
        System.out.println(client.path("sample/request/234234").accept(MediaType.TEXT_PLAIN).get(String.class));
    }
    
    @Test
    public void testBean() {
        User user = client.path("sample/bean/{id}", 25).accept(MediaType.APPLICATION_XML).get(User.class);
        System.out.println(user);
    }
    
    @Test
    public void testList() {
        System.out.println(client.path("sample/list").accept(MediaType.APPLICATION_XML).get(Users.class).getUsers());
    }
    
    @Test
    public void testMap() {
        System.out.println(client.path("sample/map").accept(MediaType.APPLICATION_XML).get(MapBean.class).getMap());
    }
    
    @Test
    public void testDeleteData() {
        client.path("sample/removeData/23").delete();
    }
    
    @Test
    public void testPostData() {
        User user = new User();
        user.setId(21432134);
        user.setAddress("hoojo#gz");
        user.setEmail("[email protected]");
        user.setName("hoojo");
        System.out.println(client.path("sample/postData").accept(MediaType.APPLICATION_XML).post(user, User.class));
    }
    
    @Test
    public void testPutData() {
        User user = new User();
        user.setId(21432134);
        System.out.println(client.path("sample/putData/1").accept(MediaType.APPLICATION_XML).put(user).getEntity());
    }
}
如果你喜欢用Spring的方式,还需要在applicationContext-client.xml中增加如下配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jaxws="http://cxf.apache.org/jaxws"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://cxf.apache.org/jaxws 
    http://cxf.apache.org/schemas/jaxws.xsd">
    
    <bean id="webClient" class="org.apache.cxf.jaxrs.client.WebClient" factory-method="create">
        <constructor-arg type="java.lang.String" value="http://localhost:8000/CXFWebService/rest/" />
    </bean>
    
</beans>

这种是利用WebClient对象来调用WebService,还有一种方法也可以调用WebService,代码如下:

// 手动创建
//RESTSample sample = JAXRSClientFactory.create("http://localhost:8000/CXFWebService/rest", RESTSample.class);
 
// 从Spring Ioc容器中拿webClient对象
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext-client.xml");
RESTSample sample = ctx.getBean("restSampleBean", RESTSample.class);
 
System.out.println(sample);
 
System.out.println(sample.doGet());
//System.out.println(sample.doRequest("haha", null, null));
System.out.println(sample.getBean(22));
System.out.println(sample.getList());
System.out.println(sample.getMap().getMap());
User user = new User();
user.setId(21432134);
user.setAddress("hoojo#gz");
user.setEmail("[email protected]");
user.setName("hoojo");
System.out.println(sample.postData(user));
System.out.println(sample.putData(111, user));
sample.deleteData(2);

这种方式相对比WebClient要简单,直接使用接口中的方法即可。同样如果你要整合到Spring可以在applicationContext-client.xml中增加配置如下:

<bean id="restSampleBean" class="org.apache.cxf.jaxrs.client.JAXRSClientFactory" factory-method="create">
    <constructor-arg type="java.lang.String" value="http://localhost:8000/CXFWebService/rest/" />
    <constructor-arg type="java.lang.Class" value="com.hoo.service.RESTSample" />
</bean>

执行以上方法可以看到控制台打印结果如下:

client console
org.apache.cxf.jaxrs.client.ClientProxyImpl@1cf7491
this is get rest request
22#JojO#null#null
com.hoo.entity.Users@16eb6bc
{key-0=0#JojO-0#null#null, key-1=1#JojO-1#null#null, key-2=2#JojO-2#null#null, key-3=3#JojO-3#null#null}
21432134#jojo##12321321#[email protected]#hoojo#gz
111#hoojo#[email protected]#hoojo#gz
 
server console
####getBean#####
id:22
Method:GET
uri:sample/bean/22
{id=[22]}
####getList#####
Method:GET
uri:sample/list
{}
####getMap#####
Method:GET
uri:sample/map
{}
21432134#hoojo#[email protected]#hoojo#gz
#####putData#####
21432134#hoojo#[email protected]#hoojo#gz
111#hoojo#[email protected]#hoojo#gz
#######deleteData#######2

就这样,整合restful WebService成功。

更多关系Spring的信息

 Spring 论坛  http://www.itchm.com/forum-59-1.html

<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
           

给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow

这里写图片描述

猜你喜欢

转载自blog.csdn.net/ugfdfgg/article/details/84194987