作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有这个 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
参数可以包含斜杠字符:例如6576/M982
。
我用 http://ip:port/samples/2000,2006,6576/M982
调用它,但显然不起作用。
我也尝试过使用 http://ip:port/samples/2000,2006,6576%2FM982
,将斜杠编码为 %2F
,但没有也不起作用,它没有到达终点。
编辑
我正在使用 Retrofit 调用端点,我这样做:
@GET("/samples/{business},{year},{sampleId}")
Observable<Sample> getSampleById(
@Path("business") String business,
@Path("year") String year,
@Path(value = "sampleId", encoded = true) String sampleId);
使用 encoded = true
,但仍然无法正常工作。
最佳答案
、
、/
等保留字符必须进行URL编码。
,
编码为%2C
/
编码为%2F
试试 http://ip:port/samples/2000%2C2006%2C6576%2FM982
。
RFC 3986定义以下一组 reserved characters可以用作分隔符。因此,它们需要 URL 编码:
: / ? # / [ ] / @ ! $ & ' ( ) * + , ; =
Unreserved characters不需要 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 - _ . ~
如果 URL 编码 ,
对您来说不是一个好的选择,您可以考虑使用查询参数。你的代码将是这样的:
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getSample(@QueryParam("business") String business,
@QueryParam("year") String year,
@QueryParam("sample") String sampleId {
...
}
您的网址将类似于 http://ip:port/samples?business=2000&year=2006&sample=6576%2FM982
。
请注意,/
仍然需要进行 URL 编码。
关于java - 如何传递包含斜杠字符的字符串路径参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38435024/
我是一名优秀的程序员,十分优秀!