一、@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
                      请求模版为:

           参数为:324
        @QueryParam:
             例如:URL地址为:http://localhost:8080/rest-resteay-demo/test/getQueryParam?id=3214
                       请求模版为:

           参数为: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参数。
 

RESTEasy常用注解的更多相关文章

  1. Spring系列之Spring常用注解总结

    传统的Spring做法是使用.xml文件来对bean进行注入或者是配置aop.事物,这么做有两个缺点:1.如果所有的内容都配置在.xml文件中,那么.xml文件将会十分庞大:如果按需求分开.xml文件 ...

  2. SpringMVC常用注解實例詳解3:@ResponseBody

    我的開發環境框架:        springmvc+spring+freemarker開發工具: springsource-tool-suite-2.9.0JDK版本: 1.6.0_29tomcat ...

  3. SpringMVC常用注解實例詳解2:@ModelAttribute

    我的開發環境框架:        springmvc+spring+freemarker開發工具: springsource-tool-suite-2.9.0JDK版本: 1.6.0_29tomcat ...

  4. Spring常用注解汇总

    本文汇总了Spring的常用注解,以方便大家查询和使用,具体如下: 使用注解之前要开启自动扫描功能 其中base-package为需要扫描的包(含子包). <context:component- ...

  5. Spring常用注解,自动扫描装配Bean

    1 引入context命名空间(在Spring的配置文件中),配置文件如下: xmlns:context="http://www.springframework.org/schema/con ...

  6. springmvc常用注解与类型转换

    springmvc常用注解与类型转换 一:前置 spring -servlet.xml 注入 <!-- 启用spring mvc 注解 --> <context:annotation ...

  7. SpringMVC常用注解,返回方式,路径匹配形式,验证

    常用注解元素 @Controller 标注在Bean的类定义处 @RequestMapping 真正让Bean具备 Spring MVC Controller 功能的是 @RequestMapping ...

  8. SpringMVC 常用注解

    本文参考了博客,具体请见:http://www.cnblogs.com/leskang/p/5445698.html Spring MVC的常用注解 1.@Controller @Controller ...

  9. spring注解开发中常用注解以及简单配置

    一.spring注解开发中常用注解以及简单配置 1.为什么要用注解开发:spring的核心是Ioc容器和Aop,对于传统的Ioc编程来说我们需要在spring的配置文件中邪大量的bean来向sprin ...

随机推荐

  1. 通过systemd配置Docker

    1. systemd Service相关目录 通常情况下,我们有3种方式可以配置etcd中的service.以docker为例,1)在目录/etc/systemd/system/docker.serv ...

  2. pymysql插入datetime类型

    第一种 create_time=datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") 第二种 update_time=time ...

  3. Ruby 循环

    Ruby while 语句: 语法: while conditional [do] codeend 执行代码当条件为true时.while循环的条件是代码中的保留字,换行,反斜杠()或一个分号隔开. ...

  4. Python程序打包成exe的一些坑

    今天写了一个项目,Python项目,需要在win7上跑起来,我想,这不是简单的不行么,直接上Pyinstaller不就完了? 但是后来,我发觉我真是too young too simple. 为什么这 ...

  5. UI控制滑杆插件

    在线演示 本地下载

  6. Hadoop 运行jar包时 java.lang.ClassNotFoundException: Class com.zhen.mr.RunJob$HotMapper not found

    错误如下 Error: java.lang.RuntimeException: java.lang.ClassNotFoundException: Class com.zhen.mr.RunJob$H ...

  7. Codeforces Round #250 (Div. 2) A, B, C

    A. The Child and Homework time limit per test 1 second memory limit per test 256 megabytes input sta ...

  8. Linux- 关于windows和Linux和Mac的换行符

    windows 的换行符为"\r\n" Linux的换行符为"\n" Mac的换行符为"\n\r",和Windows相反

  9. string 中的 length函数 和size函数 返回值问题

    string 中的 length函数 和 size函数 的返回值  (  还有 char [ ] 中 测量字符串的  strlen 函数 ) 应该是 unsigned int 类型的 不可以 和 -1 ...

  10. css中单位px和em,rem的区别

    PX:PX实际上就是像素,用PX设置字体大小时,比较稳定和精确.但是这种方法存在一个问题,当用户在浏览器中浏览我们制作的Web页面时,如果改变了浏览器的缩放,这时会使用我们的Web页面布局被打破.这样 ...