RestEasy 用户指南----第5章.@PathParam

简介:

@PathParam 的声明允许你在URI路径中去映射你的方法将使用的参数。

 

 

@Path("/library")
public class Library {

   @GET
   @Path("/book/{isbn}")
   public String getBook(@PathParam("isbn") String id) {
      // search my database and get a string representation and return it
   }
}

 

 

(很简单,当你发出get请求 /book/152-963参数152-963就在isbn中存储着,然后交给变量id,这样你的方法就算是成功的接收了该参数)

这将允许你在uri中内嵌一个变量标识符。在上边的例子中,参数isbn被用来传递book的信息。你所嵌入的数据类型可以是任何元数据类型,例如String,具有String参数的构造函

数的一个类对象,或者a static valueOf method that takes a String as a parameter。例如,假设ISBN是一个对象,我们可以

   @GET
   @Path("/book/{isbn}")
   public String getBook(@PathParam("isbn") ISBN id) {...}


   public class ISBN {
      public ISBN(String str) {...}
   }

 

或者是一个public方法String构造,包含一个valueOf 方法

 

 

  public class ISBN {            public static ISBN valueOf(String isbn) {...}   }

 

 

(运行中应该能够自动调用类的valueOf方法进行转换,对java不是很熟悉,我想大概应该是这样)


 

5.1. @PathParam深入 以及正则表达式


 

下边是一些更复杂的应用,这些在前边的章节并没有讨论

你可以指定一个或者多个参数用以内嵌到你的uri中,下边是一些例子

 

1.@Path("/aaa{param}bbb")

2.@Path("/{name}-{zip}")

3.@Path("/foo{name}-{zip}bar")


那么,路径 "/aaa111bbb" 将会匹配#1. "/bill-02115"将会匹配 #2. 路径"foobill-02115bar" 将会匹配 #3.

之前,我们已经讨论过如何在@Path中使用正则表达式

 

@GET
@Path("/aaa{param:b+}/{many:.*}/stuff")
public StringgetIt(@PathParam("param") String bs, @PathParam("many")String many) {...}

 

 

在如下的请求中,我们可以了解到“param”以及“many”值是多少

Request

param

many

GET /aaabb/some/stuff

bb

some

GET /aaab/a/lot/of/stuff

b

a/lot/of

 

5.2@PathParam 和 PathSegment

Thespecification has a very simple abstraction for examining a fragment of the URIpath being invoked on javax.ws.rs.core.PathSegment:

public interface PathSegment {

    /**
     * Get the path segment.
     * <p>
     * @return the path segment
     */
    String getPath();
    /**
     * Get a map of the matrix parameters associated with the path segment
     * @return the map of matrix parameters
     */
    MultivaluedMap<String, String> getMatrixParameters();
    
}

你可以使用resteasy注入一个PathSegment而不是用一个值

   @GET
   @Path("/book/{id}")
   public String getBook(@PathParam("id") PathSegment id) {...}

当你使用matrix parameters传递诸多参数时,浙江爱那个非常有用。你可以将任意个name和value的键值对潜入到uri path segment中。PathSegment对象将会负责去获取这些参数。

你也可以看一下MatrixParam(后边会讲到)

一个matrix parameter的例子是

 

GEThttp://host.com/library/book;name=EJB 3.0;author=Bill Burke

 

Thebasic idea of matrix parameters is that it represents resources that areaddressable by their attributes as well as their raw id.

(好吧,5.2我表示不怎么理解,自己还没有试过这个部分。)



本文转自HDDevTeam 51CTO博客,原文链接:http://blog.51cto.com/hddev/696874,如需转载请自行联系原作者

相关文章
|
2月前
|
Java 开发工具
【Azure Developer】示例: 在中国区调用MSGraph SDK通过User principal name获取到User信息,如Object ID
【Azure Developer】示例: 在中国区调用MSGraph SDK通过User principal name获取到User信息,如Object ID
swagger2 注解说明 @ApiImplicitParam和@ApiImplicitParams的用法
swagger2 注解说明 @ApiImplicitParam和@ApiImplicitParams的用法
90 0
|
Apache
Apache相关配置----用户认证
Apache相关配置----用户认证
103 1
|
Java
修改广告接口(alipay.marketing.cdp.advertise.modify)JAVA版本demo
说明:         该接口是开发者帮助线下商家修改广告内容,如修改的是线上的广告内容,需要先将线上广告内容下架,再修改,修改后操作上架,才能在支付宝钱包APP看到修改后的广告内容。运营位类型可以选择图片或H5。
402 12
|
Java
更新卡券接口(alipay.pass.instance.update)JAVA版本小样
官方接口文档:https://docs.open.alipay.com/199/105250/  发放卡券接口参考该贴:https://openclub.alipay.com/read.php?tid=2193&fid=8  使用场景介绍 对于已经发布的卡券,商户可通过此接口进行更新,更新卡券接口包含三大主要功能:核销卡券、失效卡券、更新卡券样式内容  核销卡券功能  核销卡券主要描述开发者如何将发放出去的卡券进行核销,使卡券在用户的支付宝客户端展示为已使用状态。
609 12
|
Java
操作广告接口(alipay.marketing.cdp.advertise.operate)JAVA版本demo
说明:          该接口是开发者帮助线下商家上线/下线广告内容,上线后用户可以在钱包APP上看到广告,下线后用户不能看到广告且该广告内容失效。         创建广告接口参考该贴:[url]https://openclub.
532 11
|
Java
创建广告接口(alipay.marketing.cdp.advertise.create)JAVA版本demo
说明及作用:        官网接口文档看一下:[url]https://docs.open.alipay.com/213/105272/[/url]        在口碑店铺页中,增加商家自定义区域。
507 0
|
Java
查询广告接口(alipay.marketing.cdp.advertise.query)JAVA版本demo
说明:        该接口作用是开发者可通过该接口查询广告内容和状态 代码调用示例:  //alipay.marketing.cdp.advertise.query(查询广告接口) public static void Ad_query(){ DefaultAlipayCli...
429 0