在使用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 风格的测试了。
相关推荐
压缩包中包含:jersey RESTfull最新版全部jar和官方demo
NULL 博文链接:https://smartzxy.iteye.com/blog/592843
WCFRestFull 空框架 详见我的相关博客
java restFull及示例和Maven.rar java restFull及示例和Maven.rar
SpringMVC HelloWorld Restfull结构项目。
非常好用的例子,大家多看看。去非常好用的例子,大家多看看。去
This specification defines a set of Java APIs for the development of Web services built according to the Representational State Transfer[1] (REST) architectural style. Readers are assumed to be ...
Spring Boot基础教程 RESTfull API简单项目的快速搭建
项目需要要和别人对接,对方用的是RestFul接口,Java平台的,我们是.NET平台的,需要编写RestFul调用客户端,为了测试也要相应的服务端,虽然我也会Java语言,但是不熟啊,搞个环境都要一两天。...
NULL 博文链接:https://yuhuiblog695685688425687986842568269.iteye.com/blog/2249846
restfull_php
该应用程序演示了面向服务的RESTfull应用程序的各个部分。 演示(Heroku托管) 大约需要2-3分钟才能启动实例 技术栈 零件 技术 后端郎 Java 11(带有模块) 宁静的框架 泽西岛 容器 Tomcat 9(嵌入式模式) ...
restfull+gradle+demo
主流技术struts2+spring2.5+ibaits+freemarker+restfull整合开发增删改查例子,带分页功能哦,分页和qq新闻分页一样的效果,项目结构清晰,拓展强。程序在myeclipse6.5下开发完成,jar包里面都有,直接导入就可以...
二、RESTfull api.pdf
CodeIgniter RestFull Automatic 您是否认为 repetivo 必须为所有表创建所有标准方法才能进行 Restfull 映射? 欢迎这是 CodeIgniter Restfull Automatic。 看下面它的优点: 一个完整的实现 Restfull GET、PUT、...
项目适合对struts2,spring,ibatis 和 restfull以及jquery有所了解的人员用来学习研究执行原理之用,亦可以作为项目对功能进行延伸。底层已经写好的基类。只要在此基础上拓展自己需要的功能接口就可以了,另外项目中...
restfull-api 由 NodeJS 提供支持的 Restfull API 我们需要安装 Mongo DB - - 安装 下载所需版本的 MongoDB 的二进制文件。 从下载二进制文件。 例如,要通过 shell 下载最新版本,请发出以下命令: curl -O ...
socialbooks-api 在AlgaWorks研讨会上基于Restfull API开发的应用程序
云休息 使用 Nodejs 开发 Restfull api