球衣同时消耗 application/json 和 application/x-www-form-urlencoded

Jersey consuming both application/json and application/x-www-form-urlencoded

我想要类似的东西

@POST
@Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_FORM_URLENCODED})
@Path("/")
void create(@Suspended final AsyncResponse asyncResponse,
            @ApiParam(required = true) @NotNull @Valid final CreateServiceRequest service);

所以我可以使用 JSON 和 URL 编码。但是当我使用 -d foo=bar 发出 POST 请求时,我收到 415 不受支持的格式错误。

是否可以使用同一个端点同时使用两者?如果不可能,我如何对 URL 编码的正文进行自动验证?我看到人们使用 MultivaluedMap 但这只是一张地图。我想确保提供正确的字段。

我认为 Jersey 是不可能的(至少我找不到相关的示例或文档)。 但请记住,您可以将通用逻辑提取到一个方法中,并且有两个方法具有相同的不同 @Consumes 指令。

@POST
@Consumes({MediaType.APPLICATION_JSON})
@Path("/")
void createJson(@Suspended final AsyncResponse asyncResponse,
                        @ApiParam(required = true) @NotNull @Valid final CreateServiceRequest service) {
    create(service)
}


@POST
@Consumes({MediaType.APPLICATION_FORM_URLENCODED})
@Path("/")
void createJson(@Suspended final AsyncResponse asyncResponse,
                        @ApiParam(required = true) @NotNull @Valid final CreateServiceRequest service) {
    create(service)
}