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");
  • 相关阅读:
    bootstrap-table设置表头宽度无效的解决方案
    bootstrap-table设置某列序号自增
    arguments.callee
    javascript中break和continue
    javascript数据类型
    <script>标签的属性
    javascript核心
    javascript中数据属性与访问器属性
    解决ie9以下下不支持html5和媒体查询(Media Queries)
    用firefox获取html页面元素的Xpath
  • 原文地址:https://www.cnblogs.com/huey/p/5399370.html
Copyright © 2011-2022 走看看