我有一个假装客户端看起来像这样:
@RequestMapping(method = RequestMethod.POST, value = "/breakdowns/utmMedium", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
List<Breakdown> getUtmMediumBreakdowns(
@RequestBody Record record,
@RequestParam("since") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) ZonedDateTime since,
@RequestParam("until") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) ZonedDateTime until,
@RequestParam("timezone") String timezone
);
@RequestMapping(method = RequestMethod.POST, value = "/breakdowns/utmCampaign", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
List<Breakdown> getUtmCampaignBreakdowns(
@RequestBody Record record,
@RequestParam("since") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) ZonedDateTime since,
@RequestParam("until") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) ZonedDateTime until,
@RequestParam("timezone") String timezone
);
@RequestMapping(method = RequestMethod.POST, value = "/breakdowns/utmSource", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
List<Breakdown> getUtmSourceBreakdowns(
@RequestBody Record record,
@RequestParam("since") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) ZonedDateTime since,
@RequestParam("until") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) ZonedDateTime until,
@RequestParam("timezone") String timezone
);
正如您所看到的,这三种方法完全相同,因为API路径的差异根据参数utmMedium
,utmCampaign
和utmSource
的变化而变化,因为在服务器端,我们对待它们的方式不同。
我无法更改服务器,因此我无法更改端点以接受此参数作为请求参数或其他内容。我想知道是否有办法让我仍然参数化这部分路径,所以我只有一个方法而不是三个。
您可以使用@PathVariable
注释定义其他参数,如下面的utmType
。
@RequestMapping(method = RequestMethod.POST, value = "/breakdowns/{utmType}", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
List<Breakdown> getUtmCampaignBreakdowns(
@RequestBody Record record,
@PathVariable("utmType") String utmType,
@RequestParam("since") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) ZonedDateTime since,
@RequestParam("until") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) ZonedDateTime until,
@RequestParam("timezone") String timezone
);
你需要注意的一件事是你需要指定名称utmType
,如带注释的@PathVariable("utmType")
。