Java Restful Web Services (二)——参数注解2

示例@Context及@CookieParam注解 (以下示例参考自韩陆所著《Java RESTful Web Service实战

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 pathMap = uriInfo
				.getPathParameters();
		buf.append("PATH_PARAMETERS:\n");
		iterating(buf, pathMap);

		final MultivaluedMap queryMap = uriInfo
				.getQueryParameters();
		buf.append("QUERY_PARAMETERS:\n");
		iterating(buf, queryMap);

		final List segmentList = uriInfo.getPathSegments();
		buf.append("PATH_SEGMENTS:\n");
		for (final PathSegment pathSegment : segmentList) {
			final MultivaluedMap matrix = pathSegment
					.getMatrixParameters();
			final String segmentPath = pathSegment.getPath();
			buf.append(matrix);
			buf.append(segmentPath);
		}
		buf.append("\nHEAD:\n");
		final MultivaluedMap headerMap = headers
				.getRequestHeaders();
		iterating(buf, headerMap);
		buf.append("COOKIE:\n");
		final Map kukyMap = headers.getCookies();
		final Iterator> i = kukyMap.entrySet().iterator();
		while (i.hasNext()) {
			final Entry 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 pathMap) {
		final Iterator>> i = pathMap.entrySet()
				.iterator();
		while (i.hasNext()) {
			final Entry> e = i.next();
			final String k = e.getKey();
			buf.append("key=").append(k).append(",value=");
			final List 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

Java Restful Web Services (二)——参数注解2_第1张图片



@CookieParam

Java Restful Web Services (二)——参数注解2_第2张图片


你可能感兴趣的:(前端)