路径中的日期会误导请求 URI

Date in path is misleading the request URI

我有一个 URL,它的日期格式类似于 "Unable to load rs/Service/Store/Grantor/122/5801/DUE/10/30/2017/ /true?request.preventCache=1562353357306 status: 404",其中 10/30/2017 是 java 代码中的一个日期

@GET
@Path("/dd/{sp}/{rpt}/{ter}/{date}/{grant}/{refresh}")
@Produces(MediaType.APPLICATION_JSON)

public List<ReportPeriodBean> getApprovals(@PathParam("sp") String sp,
        @PathParam("rpt") String rpt, @PathParam("ter") String ter,
        @PathParam("date") String date,
        @PathParam("grant") String grant, @PathParam("refresh") boolean refresh) throws Exception {

我应该如何让我的 URL 以正确的日期格式运行并允许控制器处理其余的事情 spring 中是否存在?

为了匹配您的描述中的 URL,您最好的选择是转义月、日和年等不同的日期部分。然后在方法内部,您可以将它们拼凑起来成为一个 Date 对象。

要将它们全部捕获为一个 Date 类型,将 运行 与 URL 结构相对照,它无法区分 Date 中的 'slashes' 与 [=22] =] 区分不同的 URL 参数。如果您不想为日期切换到 ISO-8601 表示并且不想将斜杠 %-encode 为 %2F 或使用查询字符串等

像这样的东西应该可以工作:

@GET
@Path("/dd/{sp}/{rpt}/{ter}/{month}/{day}/{year}/{grant}/{refresh}")
@Produces(MediaType.APPLICATION_JSON)
public List<ReportPeriodBean> getApprovals(@PathParam("sp") String sp,
        @PathParam("rpt") String rpt, 
        @PathParam("ter") String ter,
        @PathParam("month") int month,
        @PathParam("day") int day,
        @PathParam("year") int year,
        @PathParam("grant") String grant, 
        @PathParam("refresh") boolean refresh) {

    LocalDate date = LocalDate.of(year, month, day);
    // Now use the date however you like

}

这将使您能够将 URL 保留在似乎首选的语法中:

rs/Service/Store/Grantor/122/5801/DUE/10/30/2017/grantValue/true?request.preventCache=1562353357306