java解析带斜杠的参数,如何传递包含斜杠字符的字符串路径参数?

I have this REST resource:

@GET

@Path("{business},{year},{sample}")

@Produces(MediaType.APPLICATION_JSON)

public Response getSample(

@PathParam("business") String business,

@PathParam("year") String year,

@PathParam("sample") String sampleId {

Sample sample = dao.findSample(business, year, sampleId);

return Response.ok(sample).build();

}

sample param can contain slash character: 6576/M982, for instance.

I'm calling it with http://ip:port/samples/2000,2006,6576/M982 but does not work, obviously.

I have also tried with http://ip:port/samples/2000,2006,6576%2FM982, encoding the slash as %2F, but doesn't work either, it doesn't reach the endpoint.

EDIT

I'm using Retrofit to call the endpoint and I do this:

@GET("/samples/{business},{year},{sampleId}")

Observable getSampleById(

@Path("business") String business,

@Path("year") String year,

@Path(value = "sampleId", encoded = true) String sampleId);

With encoded = true, but still not working.

解决方案

Reserved characters such as , and / must be URL encoded.

, is encoded as %2C

/ is encoded as %2F

Try http://ip:port/samples/2000%2C2006%2C6576%2FM982.

The RFC 3986 defines the following set of reserved characters that can be used as delimiters. Hence, they require URL encoding:

: / ? # / [ ] / @ ! $ & ' ( ) * + , ; =

Unreserved characters do not require URL encoding:

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

a b c d e f g h i j k l m n o p q r s t u v w x y z

0 1 2 3 4 5 6 7 8 9 - _ . ~

If URL encoding , is not a good alternative for you, you could consider using query parameters. Your code will be like:

@GET

@Produces(MediaType.APPLICATION_JSON)

public Response getSample(@QueryParam("business") String business,

@QueryParam("year") String year,

@QueryParam("sample") String sampleId {

...

}

And your URL will be like http://ip:port/samples?business=2000&year=2006&sample=6576%2FM982.

Please note that the / still needs to be URL encoded.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值