Jersey(1.19.1) - Building URIs
A very important aspects of REST is hyperlinks, URIs, in representations that clients can use to transition the Web service to new application states (this is otherwise known as "hypermedia as the engine of application state"). HTML forms present a good example of this in practice.
Building URIs and building them safely is not easy with java.net.URI, which is why JAX-RS has the UriBuilder class that makes it simple and easy to build URIs safely.
UriBuilder can be used to build new URIs or build from existing URIs. For resource classes it is more than likely that URIs will be built from the base URI the web service is deployed at or from the request URI. The class UriInfo provides such information (in addition to further information, see next section).
The following example shows URI building with UriInfo and UriBuilder from the bookmark sample:
@Path("/users/")
public class UsersResource { @Context UriInfo uriInfo; ... @GET
@Produces("application/json")
public JSONArray getUsersAsJsonArray() {
JSONArray uriArray = new JSONArray();
for (UserEntity userEntity : getUsers()) {
UriBuilder ub = uriInfo.getAbsolutePathBuilder();
URI userUri = ub.path(userEntity.getUserid()).build();
uriArray.put(userUri.toASCIIString());
}
return uriArray;
}
}
UriInfo is obtained using the @Context annotation, and in this particular example injection onto the field of the root resource class is performed, previous examples showed the use of @Context on resource method parameters.
UriInfo can be used to obtain URIs and associated UriBuilder instances for the following URIs: the base URI the application is deployed at; the request URI; and the absolute path URI, which is the request URI minus any query components.
The getUsersAsJsonArray
method constructs a JSONArrray where each element is a URI identifying a specific user resource. The URI is built from the absolute path of the request URI by calling UriInfo.getAbsolutePathBuilder(). A new path segment is added, which is the user ID, and then the URI is built. Notice that it is not necessary to worry about the inclusion of '/' characters or that the user ID may contain characters that need to be percent encoded. UriBuilder takes care of such details.
UriBuilder can be used to build/replace query or matrix parameters. URI templates can also be declared, for example the following will build the URI "http://localhost/segment?name=value":
UriBuilder.fromUri("http://localhost/")
.path("{a}")
.queryParam("name", "{value}")
.build("segment", "value");
Jersey(1.19.1) - Building URIs的更多相关文章
- Jersey(1.19.1) - Building Responses
Sometimes it is necessary to return additional information in response to a HTTP request. Such infor ...
- Jersey(1.19.1) - Client API, Ease of use and reusing JAX-RS artifacts
Since a resource is represented as a Java type it makes it easy to configure, pass around and inject ...
- Jersey(1.19.1) - Hello World, Get started with Jersey using the embedded Grizzly server
Maven Dependencies The following Maven dependencies need to be added to the pom: <dependency> ...
- Jersey(1.19.1) - Hello World, Get started with a Web application
1. Maven Dependency <properties> <jersey.version>1.19.1</jersey.version> </prop ...
- Jersey(1.19.1) - Root Resource Classes
Root resource classes are POJOs (Plain Old Java Objects) that are annotated with @Path have at least ...
- Jersey(1.19.1) - Client API, Overview of the API
To utilize the client API it is first necessary to create an instance of a Client, for example: Clie ...
- Jersey(1.19.1) - Client API, Using filters
Filtering requests and responses can provide useful functionality that is hidden from the applicatio ...
- Jersey(1.19.1) - JSON Support
Jersey JSON support comes as a set of JAX-RS MessageBodyReader<T> and MessageBodyWriter<T&g ...
- Jersey(1.19.1) - Deploying a RESTful Web Service
JAX-RS provides a deployment agnostic abstract class Application for declaring root resource and pro ...
随机推荐
- Nginx 禁止IP访问
我们在使用的时候会遇到很多的恶意IP攻击,这个时候就要用到Nginx 禁止IP访问了.下面我们就先看看Nginx的默认虚拟主机在用户通过IP访问,或者通过未设置的域名访问(比如有人把他自己的域名指向了 ...
- WEB安全之威胁解析
本文章转载自 http://www.xuebuyuan.com/60198.html 主要威胁: 暴力攻击(brute-force attack):这些攻击通过尝试所有可能的字符组合,以发现用户证书. ...
- 开发WebForm时遇到的问题
在做一个小项目时,一个很长的页面,页面底部有一个contact us form 整个页面我没有使用MVC,而是使用ASP.NET WebForm(.aspx)来实现,实现功能后发现,当用户在页面底部输 ...
- Windows 8 之 windbg 配置
怎么安装windbg? 在Win8中,要通过安装windows 8 SDK来安装. 安装之后,在C:\Program Files (x86)\Windows Kits\8.0\Debuggers\x6 ...
- MATLAB remove outliers.
Answer by Richard Willey on 9 Jan 2012 Hi Michael MATLAB doesn't provide a specific function to remo ...
- cocos2d-x 图形绘制
转自:http://blog.csdn.net/zhy_cheng/article/details/8480048 图形绘制的话,在cocos2d-x自带的TestCpp里有,包括绘制点,直线,多边形 ...
- UVa712 S-Trees
// UVa712 S-Trees // Rujia Liu // 题意:给一棵满二叉树,每一层代表一个01变量,取0时往左走,取1时往右走.给出所有叶子的值,以及一些查询(即每个变量的值),求最后到 ...
- IIS应用程序池性能分析
#查看应用程序池和w3wp.exe进程的对应关系iisapp -a C:\windows\system32\inetsrv\appcmd.exe list wp 查看任务管理器: 在性能计数器中找到对 ...
- linux自己带的apache重新启动
如果是linux自己带的apache的话就使用命令 service httpd start 启动 service httpd stop 关闭 service httpd restart 重新启动 如果 ...
- TC SRM 664 div2 A BearCheats 暴力
BearCheats Problem Statement Limak is an old brown bear. Because of his bad eyesight he sometime ...