REST培训文档.doc

上传人:11****ws 文档编号:2993501 上传时间:2019-05-16 格式:DOC 页数:10 大小:29.60KB
下载 相关 举报
REST培训文档.doc_第1页
第1页 / 共10页
REST培训文档.doc_第2页
第2页 / 共10页
REST培训文档.doc_第3页
第3页 / 共10页
REST培训文档.doc_第4页
第4页 / 共10页
REST培训文档.doc_第5页
第5页 / 共10页
点击查看更多>>
资源描述

1、REST 培训教程Jar 准备asm-3.3.1.jargrizzly-http-all-2.2.1.jarjersey-bundle-1.12.jarjsr311-api-1.1.1.jarjavax.servlet-3.1.jarorg.restlet.jar上面 6 个 jar 文件环境搭建JDK 版本必须是 1.6 版本 build 1.6.0_24-b07培训的 tomcat 版本:apache-tomcat-6.0.33将上述 jar 文件发布到项目的 WEB-INFlib 目录下在 web.xml 中添加JAX-RS REST ServletJAX-RS REST Servlet

2、com.sun.jersey.spi.container.servlet.ServletContainercom.sun.jersey.config.property.packagescom.ailk.toptea.sysmgr.upload.rest 4JAX-RS REST Servlet/rest/*注意:com.sun.jersey.config.property.packages 是我们开发 rest 程序的包路径JAX-RS REST Servlet 名字可以根据自己的设计起实例创建工程我们在 ecplise 中创建一个 rest.sample 工程,注意工程使用的 jdk 一定要

3、是 jdk1.6 的版本,创建用户库名称使用 rest 并将上述依赖的 jar 文件导入库中。创建包 com.sample.rest创建类 SampleRest编写代码package com.sample.rest;import javax.ws.rs.GET;import javax.ws.rs.Path;Path(“sample“)public class SampleRest Path 就是 HTTP 地址下面介绍具体的例子PATH 的用户测试方法:直接地址栏键入地址无 path 的方法GETpublic String getString()return “rest success“;在

4、 IE 地址栏中键入 http:/127.0.0.1:10000/sample1/rest/sample我们可以看到rest success有 path 的方法GETPath(“path1”)public String getString1()return “rest success path“;在 IE 地址栏中键入 http:/127.0.0.1:10000/sample1/rest/sample/path1我们可以看到rest success path几种请求方式测试方法教程中提供了 restform.html 文件可以测试(无参数的测试可以通过改文件测试)GET 方式在上介绍 path

5、 的时候已经说明,就不再具体说明了 POST 方式POSTpublic String getStringByPost()return “rest success post“;restform.html 中输入 http:/127.0.0.1:10000/sample1/rest/sample/结果返回rest success postDELETE 方式暂时没有使用PUT 方式暂时没有使用参数传递方式测试方式我们利用 jersey 中的 http 请求类模仿 url 请求QueryParam URL 后面跟参数方式获取例子GETPath(“param1“)public String getStr

6、ingByParam1(QueryParam(“name“) String name)return name;测试方法public static void testGetStringByQueryParam ()Client client = Client.create();WebResource webResource2 = client.resource(“http:/127.0.0.1:10000/sample1/rest/sample/param1“);MultivaluedMap queryParams = new MultivaluedMapImpl();queryParams.a

7、dd(“name“, “test name“);String a = webResource2.queryParams(queryParams).get(String.class);System.out.print(a);FormParam表单方式例子GETPath(“param2“)public String getStringByParam2(FormParam(“name“) String name)return name;测试方法public static void testGetStringByFormParam ()Client client = Client.create();W

8、ebResource webResource2 = client.resource(“http:/127.0.0.1:10000/sample1/rest/sample/param2“);MultivaluedMap queryParams = new MultivaluedMapImpl();queryParams.add(“name“, “form name“);String a = webResource2.post(String.class,queryParams);System.out.print(a);PathParam路径方式例子1. GET 2. Path(“/book/isb

9、n“) 3. public String getBook(PathParam(“isbn“) String id) 4. / search my database and get a string representation and return it 5. 测试方法public static void testGetStringByPathParam ()Client client = Client.create();WebResource webResource2 = client.resource(“http:/127.0.0.1:10000/sample1/rest/sample/p

10、aram3/dsdsdsldskl“);String a = webResource2.get(String.class);System.out.print(a);MatrixParam路径方式例子GET Path(“/param4“) public String getStringByParam4(MatrixParam(“name“) String name, MatrixParam(“author“) String author) return name+author;测试public static void testGetStringByMatrixParam ()Client cli

11、ent = Client.create();WebResource webResource2 = client.resource(“http:/127.0.0.1:10000/sample1/rest/sample/param4;name=EJB;author=BillBurke“);String a = webResource2.get(String.class);System.out.print(a);Context 动态方式获取GET Path(“/param5“) public String getStringByParam5(Context UriInfo info) String

12、from = info.getQueryParameters().getFirst(“from“); String to = info.getQueryParameters().getFirst(“to“); List orderBy = info.getQueryParameters().get(“orderBy“); return “form=“+from+“;to=“+to+“;orderBy-length=“+orderBy.size();测试public static void testGetStringByContext ()Client client = Client.creat

13、e();WebResource webResource2 = client.resource(“http:/127.0.0.1:10000/sample1/rest/sample/param5?from=100String a = webResource2.get(String.class);System.out.print(a);DefaultValue 初始值GET Path(“/param6“) public String getStringByParam6( DefaultValue(“sddsds“) QueryParam(“name“) String name) return na

14、me;测试public static void testGetStringByDefaultValue ()Client client = Client.create();WebResource webResource2 = client.resource(“http:/127.0.0.1:10000/sample1/rest/sample/param6“);String a = webResource2.get(String.class);System.out.println(a);MultivaluedMap queryParams = new MultivaluedMapImpl();q

15、ueryParams.add(“name“, “test name“);a = webResource2.queryParams(queryParams).get(String.class);System.out.println(a);HeaderParam 头参数GET Path(“/param7“) public String getStringByParam7( Context HttpHeaders headers) String userAgent = headers.getRequestHeader(“user-agent“).get(0); return userAgent;测试

16、public static void testGetStringByHeaderParam()Client client = Client.create();WebResource webResource2 = client.resource(“http:/127.0.0.1:10000/sample1/rest/sample/param7“);String a = webResource2.get(String.class);System.out.print(a);自定义类参数POST Path(“/param8“) public String getStringByParam8(User

17、user) return user.getId()+“+user.getName(); 测试public static void testGetStringByObject()Client client = Client.create();WebResource webResource2 = client.resource(“http:/127.0.0.1:10000/sample1/rest/sample/param8“);User u = new User();u.setId(“panhua“);u.setName(“潘华“);String a = webResource2.post(St

18、ring.class,u);System.out.print(a);返回方式String 上述方式全部为 String 返回就不再举例JSONGET Path(“/param9“) Produces(“application/json“)public User getStringByParam9() User u = new User();u.setId(“panhua“);u.setName(“潘华“);return u;测试public static void testGetJson()Client client = Client.create();WebResource webResou

19、rce2 = client.resource(“http:/127.0.0.1:10000/sample1/rest/sample/param9“);String a = webResource2.get(String.class);System.out.print(a);XMLGET Path(“/param10“) Produces(“application/xml“)public User getStringByParam10() User u = new User();u.setId(“panhua“);u.setName(“潘华“);return u;测试public static

20、void testGetXML()Client client = Client.create();WebResource webResource2 = client.resource(“http:/127.0.0.1:10000/sample1/rest/sample/param10“);String a = webResource2.get(String.class);System.out.print(a);对象可以用 json 和 XML 直接放回对象测试方法public static void testGetObject()Client client = Client.create();

21、WebResource webResource2 = client.resource(“http:/127.0.0.1:10000/sample1/rest/sample/param9“);User a = webResource2.get(User.class);System.out.print(a.getName();Spring 的调用Context ServletContext context;GET Path(“/param11“) Produces(“application/json“)public User getStringByParam11() WebApplicationC

22、ontext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(context);TestSpring ur = (TestSpring)ctx.getBean(“testSpring“);return ur.getUser();测试public static void testGetObjectBySpring()Client client = Client.create();WebResource webResource2 = client.resource(“http:/127.0.0.1:10000/sample1/rest/sample/param11“);User a = webResource2.get(User.class);System.out.print(a.getName();

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 实用文档资料库 > 策划方案

Copyright © 2018-2021 Wenke99.com All rights reserved

工信部备案号浙ICP备20026746号-2  

公安局备案号:浙公网安备33038302330469号

本站为C2C交文档易平台,即用户上传的文档直接卖给下载用户,本站只是网络服务中间平台,所有原创文档下载所得归上传人所有,若您发现上传作品侵犯了您的权利,请立刻联系网站客服并提供证据,平台将在3个工作日内予以改正。