zoukankan      html  css  js  c++  java
  • RESTEasy常用注解

    一、@Path,标注资源类或方法的相对路径
             Path参数的形式有三种:
             1、固定值
             2、纯正则表达式
             3、固定值和正则表达式的混合体
    /**
    * @功能描述: (Path中的参数可以是固定值)
    */
    @GET
    @Path("test-get-param")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, Object> getNotParam() {
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("say:", new Date());
    return map;
    }
     
    /**
    * @功能描述: (Path中的参数可以是正则表达式)
    */
    @Path("{id}")
    @GET
    @Produces("text/plain; charset=utf-8")
    public String getPathParam(@PathParam(value = "id") int id) {
    return "您好,您输入的数字为:" + id;
    }
     
    /**
    * @功能描述: (Path中的参数可以是多个正则表达式的组合)
    */
    @Path("{first}_{last}")
    @GET
    @Produces("application/json; charset=utf-8")
    public String getMorePathParam(@PathParam(value = "first") String first,
    @PathParam(value = "last") String last) {
    return "输入的信息为,first:" + first + ";last:" + last;
    }
    

      


     
    二、@GET,@PUT,@POST,@DELETE,标注方法是用的HTTP请求的类型 
            @GET  : 提供查询方法,所有参数均在URL路径中,只能传输一个或者多个字符串,无法传输对象
            @POST:提供新增方法,参数可以存在URL路径中,也可以存在BODY中。
           如传输文本格式的参数,使用String类型或者基本数据类型;
                             如传输JSON格式的参数,使用map、list或者对象。
            @PUT  : 提供修改方法
            @DELETE:提供删除方法
    三、@Produces,标注返回的MIME媒体类型
                                 处理返回中文乱码:@Produces("text/plain; charset=utf-8")
           @Consumes,标注可接受请求的MIME媒体类型
    四、标记Http请求不同位置:
           @PathParam,@QueryParam,@HeaderParam,@CookieParam,@MatrixParam,@FormParam
           @PathParam:提取URL模版路径中的参数
               例如:URL地址为:http://localhost:8080/rest-resteay-demo/test/324
                          请求模版为:
     
        @Path("{id}")
        @GET
        @Produces("text/plain; charset=utf-8")
        public String getPathParam(@PathParam(value = "id") int id) {
        return "您好,您输入的数字为:" + id;
        }
               参数为:324
            @QueryParam:
                 例如:URL地址为:http://localhost:8080/rest-resteay-demo/test/getQueryParam?id=3214
                           请求模版为:
     
    @Path("getQueryParam")
    @GET
    @Produces("text/plain; charset=utf-8")
    public String getQueryParam(@QueryParam(value = "id") String id) {
            return "请求信息为:" + id;
    }
               参数为:43214
              @MatrixParam:GET方式请求时获取路径中与Path正则表达式多出不一致的参数
                  例如:URL地址为:http://localhost:8080/RestEasy/test/test--context;color=balck
                             请求模版为:
     
    
    
    /**
    * @功能描述: (使用MatrixParam参数,在使用正则表达式方式入参时,
    * 部分参数和Path中无法匹配时,使用MatrixParam获取)
    */
    @GET
    @Path("{model}--{year}")
    @Produces("text/plain; charset=utf-8")
    public String getMatrixParam(@MatrixParam(value = "color") String color,
    @PathParam(value = "year") String year,
    @PathParam(value = "model") String model) {
     
    return "color: " + color + "; year: " + year + "; model: " + model;
    }
                         参数为:color: black; year: context; model: test
                @Context:获取各种类型请求参数
                    例如:请求路径为:http://localhost:8080/RestEasy/test/test-context/123;color=balack
                               模版样例为:
    
    
    /**
    * @功能描述: (Context获取Path路径,Matrix参数,PathParam参数)
    */
    @GET
    @Path("test-context/{id}")
    @Produces("text/plain; charset=utf-8")
    public String getContext(@Context UriInfo uriInfo) {
    String path = uriInfo.getPath();
    List<PathSegment> lsps = uriInfo.getPathSegments();
    String psString = "";
    for (PathSegment ps : lsps) {
    psString = psString + JSON.toJSONString(ps) + "; ";
    }
    MultivaluedMap<String, String> map = uriInfo.getPathParameters();
    return "path:" + path + "; lsps:" + psString + "; map:"
    + JSON.toJSONString(map);
    }    <span style="color:#808080;"> </span><span style="font-family:'Microsoft YaHei UI';font-size:10.5pt; line-height:1.5">    </span>
                          参数为:path:/test/test-context/123;color=balack; 
                                             lsps:{"matrixParameters":{},"original":"test","path":"test"};
                                                    {"matrixParameters":{},"original":"test-context","path":"test-context"}; 
                                                    {"matrixParameters":{"color":["balack"]},"original":"123;color=balack","path":"123"}; 
                                             map:{"id":["123"]}
                                             其中:original: 表示原文,Path:表示路径,MatrixParam:表示Matrix参数。
     
  • 相关阅读:
    用户需求报告
    结队开发项目——七巧板NABC需求分析
    梦断代码读书笔记3
    课堂练习之环形二维数组
    结对开发之求最大子数组的溢出问题
    《代码之美》第二章读后感(一)
    软件工程项目冲刺阶段二:第五天
    软件工程项目冲刺阶段二:第四天(06-06)
    软件工程项目冲刺阶段二:第三天
    课程评价
  • 原文地址:https://www.cnblogs.com/xiohao/p/4771533.html
Copyright © 2011-2022 走看看