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

    Previous sections on @Produces and @Consumes referred to MIME media types of representations and showed resource methods that consume and produce the Java type String for a number of different media types. However, String is just one of many Java types that are required to be supported by JAX-RS implementations.

    Java types such as byte[], java.io.InputStream, java.io.Reader and java.io.File are supported. In addition JAXB beans are supported. Such beans are JAXBElement or classes annotated with @XmlRootElement or @XmlType. The samples jaxb and json-from-jaxb show the use of JAXB beans.

    Unlike method parameters that are associated with the extraction of request parameters, the method parameter associated with the representation being consumed does not require annotating. A maximum of one such unannotated method parameter may exist since there may only be a maximum of one such representation sent in a request.

    The representation being produced corresponds to what is returned by the resource method. For example JAX-RS makes it simple to produce images that are instance of File as follows:

    @GET
    @Path("/images/{image}")
    @Produces("image/*")
    public Response getImage(@PathParam("image") String image) {
        if (!isSafeToOpenFile(image)) {
            throw new IllegalArgumentException("Cannot open the image file.");
        }
    
        File file = new File(image);
        if (!file.exists()) {
            throw new WebApplicationException(404);
        }
    
        String mimeType = new MimetypesFileTypeMap().getContentType(file);
        return Response.ok(file, mimeType).build();
    }

    File type can also be used when consuming, a temporary file will be created where the request entity is stored.

    The Content-Type (if not set, see next section) can be automatically set from the MIME media types declared by @Produces if the most acceptable media type is not a wild card (one that contains a *, for example "application/" or "/*"). Given the following method:

    @GET
    @Produces({"application/xml", "application/json"})
    public String doGetAsXmlOrJson() {
        ...
    }

    if "application/xml" is the most acceptable then the Content-Type of the response will be set to "application/xml".

  • 相关阅读:
    准备工作
    使用awstats分析nginx日志
    kvm虚拟化环境中的时区设置
    使用awk格式化输出文本
    gitlab(7.9)升级到8.0.1
    为openstack制作CoreOS虚拟机镜像(基于CoreOS官方提供镜像)
    KVM虚拟化之嵌套虚拟化nested
    编译制作Linux 3.18内核rpm包(升级centos6.x虚拟机内核)
    Linux主机之间传输文件的几种方法对比
    spice在桌面虚拟化中的应用系列之二(Linux平台spice客户端的编译安装,支持USB映射)
  • 原文地址:https://www.cnblogs.com/huey/p/5398533.html
Copyright © 2011-2022 走看看