泽西岛消耗应用程序/JSON和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提出发布请求时,我会收到415个不受支持的格式错误。

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

我相信球衣是不可能的(至少我找不到示例或文档)。但是请记住,您可以将常见的逻辑提取到一种方法中,并且具有不同的@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)
}

相关内容

最新更新