zoukankan      html  css  js  c++  java
  • Jersey(1.19.1)

    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");
  • 相关阅读:
    将文件写进数据库的方法
    立个Flag
    JQuery_学习1
    js制作一个简单的选项卡
    输出数据库中的表格的内容(pdo连接)
    不饮鸡汤的寂寞先生
    详细谈Session
    详细谈Cookie
    php字符串操作函数练习2
    ios开发网络学习五:MiMEType ,多线程下载文件思路,文件的压缩和解压缩
  • 原文地址:https://www.cnblogs.com/huey/p/5399370.html
Copyright © 2011-2022 走看看