Java Restful Web Services (二)——参数注解2
2015-03-02 20:28
295 查看
示例@Context及@CookieParam注解 (以下示例参考自韩陆所著《Java RESTful Web Service实战》)
测试类
测试案例执行结果:
@Context
@CookieParam
import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.*; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Map.Entry; @Path("ctx-resource") public class ContextResource { @GET @Path("{region:.+}/kaifeng/{district:\\w+}") @Produces(MediaType.TEXT_PLAIN) public String getByAddress(@Context final Application application, @Context final Request request, @Context final javax.ws.rs.ext.Providers provider, @Context final UriInfo uriInfo, @Context final HttpHeaders headers) { final StringBuilder buf = new StringBuilder(); final String path = uriInfo.getPath(); buf.append("PATH=").append(path).append("\n"); final MultivaluedMap<String, String> pathMap = uriInfo .getPathParameters(); buf.append("PATH_PARAMETERS:\n"); iterating(buf, pathMap); final MultivaluedMap<String, String> queryMap = uriInfo .getQueryParameters(); buf.append("QUERY_PARAMETERS:\n"); iterating(buf, queryMap); final List<PathSegment> segmentList = uriInfo.getPathSegments(); buf.append("PATH_SEGMENTS:\n"); for (final PathSegment pathSegment : segmentList) { final MultivaluedMap<String, String> matrix = pathSegment .getMatrixParameters(); final String segmentPath = pathSegment.getPath(); buf.append(matrix); buf.append(segmentPath); } buf.append("\nHEAD:\n"); final MultivaluedMap<String, String> headerMap = headers .getRequestHeaders(); iterating(buf, headerMap); buf.append("COOKIE:\n"); final Map<String, Cookie> kukyMap = headers.getCookies(); final Iterator<Entry<String, Cookie>> i = kukyMap.entrySet().iterator(); while (i.hasNext()) { final Entry<String, Cookie> e = i.next(); final String k = e.getKey(); buf.append("key=").append(k).append(",value="); final Cookie cookie = e.getValue(); buf.append(cookie.getValue()).append(" "); buf.append("\n"); } return buf.toString(); } private void iterating(final StringBuilder buf, final MultivaluedMap<String, String> pathMap) { final Iterator<Entry<String, List<String>>> i = pathMap.entrySet() .iterator(); while (i.hasNext()) { final Entry<String, List<String>> e = i.next(); final String k = e.getKey(); buf.append("key=").append(k).append(",value="); final List<String> vList = e.getValue(); for (final String v : vList) { buf.append(v).append(" "); } buf.append("\n"); } } }
import javax.ws.rs.CookieParam; import javax.ws.rs.GET; import javax.ws.rs.Path; @Path("cookie-resource") public class CookieResource { @GET public String getHeaderParams( @CookieParam("longitude") final String longitude, @CookieParam("latitude") final String latitude, @CookieParam("population") final double population, @CookieParam("area") final int area) { return longitude + "," + latitude + " population=" + population + ",area=" + area; } }
测试类
import javax.ws.rs.client.WebTarget; import org.junit.After; import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; public class ContextResourceTest { @BeforeClass public static void setUpBeforeClass() throws Exception { } @AfterClass public static void tearDownAfterClass() throws Exception { } @Before public void setUp() throws Exception { } @After public void tearDown() throws Exception { } @Test public void testGetByAddress() { final String path = "ctx-resource"; String result; final WebTarget pathTarget = Testter.target.path(path).path("Asia") .path("China").path("northeast").path("henan").path("kaifeng") .path("gulou"); result = pathTarget.request().get().readEntity(String.class); System.out.println(result); final WebTarget queryTarget = Testter.target.path(path).path("China") .path("kaifeng").path("gulou") .queryParam("station", "Workers Village") .queryParam("vehicle", "bus"); result = queryTarget.request().get().readEntity(String.class); System.out.println(result); } }
import javax.ws.rs.client.Invocation.Builder; import org.junit.After; import org.junit.AfterClass; import org.junit.Assert; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; public class CookieResourceTest { @BeforeClass public static void setUpBeforeClass() throws Exception { } @AfterClass public static void tearDownAfterClass() throws Exception { } @Before public void setUp() throws Exception { } @After public void tearDown() throws Exception { } @Test public void testGetHeaderParams() { // fail("Not yet implemented"); final Builder request = Testter.target.path("cookie-resource") .request(); request.cookie("longitude", "123.38"); request.cookie("latitude", "41.8"); request.cookie("population", "822.8"); request.cookie("area", "12948"); String result = request.get().readEntity(String.class); System.out.println(result); Assert.assertEquals("123.38,41.8 population=822.8,area=12948", result); } }
测试案例执行结果:
@Context
@CookieParam
相关文章推荐
- Java Restful Web Services (二)——参数注解1
- Java Restful Web Services (三)——方法注解
- JAX-RS(Java API for RESTful Web Services)常用注解
- jax-rs(Java API for RESTful Web Services) 实践教程 之四 —— @Context注入HttpServletRequest 使REST保持状态!
- 在Java上实现RESTful Web Services
- JAX-RS介绍——Java API forRESTful WebServices,JAX-RS的目标是Web Services开发(这与HTML Web应用不同)而Spring MVC的目标则是Web应用开发
- RESTful Web Services -- JAX-RS中的注解
- jax-rs(Java API for RESTful Web Services) 实践教程 之五 —— 注入全局变量 和 rest的生命周期
- Jersey - RESTful Web Services in Java.
- Jersey -RESTful Web Services in Java基础框架示例
- java端RESTful web services的几种实现方式
- java端RESTful web services的几种实现方式
- jboss7 Java API for RESTful Web Services (JAX-RS) 官方文档
- java搭建RESTful WebServices 快速入门(一)
- jax-rs(Java API for RESTful Web Services)实践教程 之二 —— get/post/put/delete/head
- RESTful Webservices with Java (Jersey / JAX-RS)
- Java Restful Web Services (一)
- JAVAWEB开发之SpringMVC详解(二)——高级开发、数据回显、参数绑定集合、图片上传、json交互、validation校验、异常处理、RESTful支持、拦截器
- JAX-RS -- Java API for RESTful Web Services
- Jersery——RESTful Web Services in Java,RESTful服务,Jersey+Spring实现REST服务调用