java 如何传递包含斜杠字符的字符串路径参数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38435024/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I pass string path param containing slash character?
提问by Héctor
I have this REST resource:
我有这个 REST 资源:
@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.
sample
param 可以包含斜杠字符:6576/M982
,例如。
I'm calling it with http://ip:port/samples/2000,2006,6576/M982
but does not work, obviously.
我正在调用它,http://ip:port/samples/2000,2006,6576/M982
但显然不起作用。
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.
我也尝试过http://ip:port/samples/2000,2006,6576%2FM982
,将斜杠编码为%2F
,但也不起作用,它没有到达端点。
EDIT
编辑
I'm using Retrofit to call the endpoint and I do this:
我正在使用 Retrofit 调用端点,我这样做:
@GET("/samples/{business},{year},{sampleId}")
Observable<Sample> getSampleById(
@Path("business") String business,
@Path("year") String year,
@Path(value = "sampleId", encoded = true) String sampleId);
With encoded = true
, but still not working.
有了encoded = true
,但仍然没有工作。
回答by cassiomolin
Reserved characters such as ,
and /
must be URL encoded.
保留字符,例如,
和/
必须经过 URL 编码。
,
is encoded as%2C
/
is encoded as%2F
,
被编码为%2C
/
被编码为%2F
Try http://ip:port/samples/2000%2C2006%2C6576%2FM982
.
试试http://ip:port/samples/2000%2C2006%2C6576%2FM982
。
The RFC 3986defines the following set of reserved charactersthat can be used as delimiters. Hence, they require URL encoding:
在RFC 3986个定义了以下一组保留的字符可被用作分隔符。因此,它们需要 URL 编码:
: / ? # / [ ] / @ ! $ & ' ( ) * + , ; =
Unreserved charactersdo not require URL encoding:
非保留字符不需要 URL 编码:
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:
如果 URL 编码,
不适合您,您可以考虑使用查询参数。您的代码将类似于:
@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
.
你的 URL 将类似于http://ip:port/samples?business=2000&year=2006&sample=6576%2FM982
.
Please note that the /
still needs to be URL encoded.
请注意,/
仍然需要进行 URL 编码。
回答by steva1988
Try to use {sample:.+}instead of {sample}
尝试使用{sample:.+}而不是{sample}
The @Path annotation is a regular expression and regex do not match /
character.
@Path 注释是一个正则表达式,正则表达式不匹配/
字符。
To override the regex, we can add ".+
at the end of the PathParam
.
要覆盖正则表达式,我们可以".+
在PathParam
.
In this way we can allow /
in our path and avoid using %2F.
通过这种方式,我们可以允许/
我们的路径并避免使用 %2F。