REST WebService简单应用(测试)

      在项目中许多同事采用Ajax调用REST Web服务比较费时,今天发现REST WebService的测试框架,Jersey-Test的使用提供一种简单快捷的测试WebService的方式。、

 

demo的结构如下:

 

package com.easyway.rest.ws;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
/**
 * 在Java App中 REST的开发应用
 * @author longgangbai
 *
 */
@Path("/helloWorld")
public class HelloWorldWS {
	
	/**
	 * 发布服务的方法
	 * @return
	 */
	@GET()
	@Produces(MediaType.TEXT_PLAIN)
	public String helloworld(){
		return "HelloWorld REST";
	}

}

 

 

package com.easyway.rest.ws;

import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.core.header.MediaTypes;
import com.sun.jersey.test.framework.JerseyTest;
import com.sun.jersey.test.framework.WebAppDescriptor;
import org.junit.Assert;
import org.junit.Test;

/**
 *采用 jersey的test框架以Web形式测试 
 * 必须继承自JerseyTest 测试
 *
 * @author longgangbai
 */
public class HelloWorldWebAppTest extends JerseyTest {


 /**
  * 初始化JerseyTest的web扫描目录和服务发布目录
  * @throws Exception
  */
    public HelloWorldWebAppTest() throws Exception {
        super(new WebAppDescriptor.Builder("com.easyway.rest.ws")
                .contextPath("helloworld-webapp").build());
    }
    /**
     * Test that the expected response is sent back.
     * @throws java.lang.Exception
     */
    @Test
    public void testHelloWorld() throws Exception {
        WebResource webResource = resource();
        String responseMsg = webResource.path("helloWorld").get(String.class);
        System.out.println("responseMsg ="+responseMsg);
        Assert.assertEquals("HelloWorld REST", responseMsg);
    }
    @Test
    public void testApplicationWadl() {
        WebResource webResource = resource();
        String serviceWadl = webResource.path("application.wadl").
                accept(MediaTypes.WADL).get(String.class);
        Assert.assertTrue(serviceWadl.length() > 0);
    }
}

 

 

package com.easyway.rest.ws;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import com.sun.grizzly.http.SelectorThread;
import com.sun.jersey.api.container.grizzly.GrizzlyWebContainerFactory;
/**
 * 在Java App中采用 Grizzly 的web容器发布WebService服务
 * 
 * @author longgangbai
 * 
 */
public class PublishWS {
	
	public static void main(String[] args){
		//发布路径
		final String baseUri = "http://localhost:9998/";
		//发布容器需要的参数
		final Map<String, String> initParams =new HashMap<String, String>();
		//相关的配置信息(在Web项目参数配置在web.xml中的参数)
		initParams.put("com.sun.jersey.config.property.packages", 
		                   "com.easyway.rest.ws");
	    System.out.println("Starting grizzly...");
	    SelectorThread threadSelector;
		try {
			//使用Grizzly容器发布相关的WebService服务
			threadSelector = GrizzlyWebContainerFactory.create(baseUri, initParams);
		   System.out.println(String.format(
		           "Jersey app started with WADL available at %sapplication.wadl\n" + 
		           "Try out %shelloworld\nHit enter to stop it...", baseUri, baseUri));
		   //用于控制web service的发布
	       System.in.read();
	       //停止发布状态
	       threadSelector.stopEndpoint();
	       //销毁
	       System.exit(0);
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值