`

jersey RestFull开发模式

阅读更多

在使用jersey前,需要添加jersey的pom依赖,其中包含了jersey与grizzly2集成测试依赖

 

		<dependency>
			<groupId>com.sun.jersey</groupId>
			<artifactId>jersey-server</artifactId>
			<version>1.9.1</version>
		</dependency>
		
		<dependency>
			<groupId>com.sun.jersey</groupId>
			<artifactId>jersey-grizzly2</artifactId>
			<version>1.9.1</version>
		</dependency>
		 <dependency>
               			 <groupId>com.sun.jersey.jersey-test-framework</groupId>
               			 <artifactId>jersey-test-framework-grizzly2</artifactId>
              			 <version>1.9.1</version>
               			 <scope>test</scope>
        		 </dependency>
		 <dependency>
			<groupId>com.sun.jersey</groupId>
			<artifactId>jersey-client</artifactId>
			 <version>1.16</version>
		</dependency>

 

 

首先需要启动

public class Main{  
     
	private static URI getBaseURI() {
		return UriBuilder.fromUri("http://localhost/").port(9998).build();
	}
	
	public static final URI BASE_URI = getBaseURI();
	
	protected static HttpServer startServer() throws IOException {
		System.out.println("Starting grizzly...");
		ResourceConfig rc = new PackagesResourceConfig("com.resource");//这个地址是rest类的目录
		return GrizzlyServerFactory.createHttpServer(BASE_URI, rc);
	}
    public static void main(String[] args) throws IOException {
    	/**
    	 * 启动单例SpringContext
    	 * 在服务start之前装载,第一次加载较慢,避免并发访问带来的加载spring异常
    	 */
    	SpringUtils.start();
    	
    	/**
    	 * 启动Grizzly web Server
    	 */
    	HttpServer httpServer = startServer();
    	System.out.println(String.format("Jersey app started with WADL available at "
    	                   + "%sapplication.wadl\nHit enter to stop it...",
    	                   BASE_URI, BASE_URI));
    	System.in.read();
    	httpServer.stop();
    }           
}
 HelloWorldResource.java
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

// The Java class will be hosted at the URI path "/helloworld"
@Path("/helloworld")
public class HelloWorldResource {

    // The Java method will process HTTP GET requests
    @GET 
    // The Java method will produce content identified by the MIME Media
    // type "text/plain"
    @Produces("text/plain")
    public String getClichedMessage() {
        // Return some cliched textual content
        return "Hello World";
    }
}
HelloWorldResourceTest.java

 

import static org.junit.Assert.*;

import org.junit.Test;

import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.test.framework.JerseyTest;

public class HelloWorldResourceTest extends JerseyTest {

    public HelloWorldResourceTest()throws Exception {
        super("com.resource");");//这个地址是rest类的目录
    }

    @Test
    public void testHelloWorld() {
        WebResource webResource = resource();
        String responseMsg = webResource.path("helloworld").get(String.class);
        assertEquals("Hello World", responseMsg);
    }

}

 

这时使用junit运行HelloWorldResourceTest.java就可以运行rest 风格的测试了。

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics