Java 同时使用application/json和application/x-www-form-urlencoded

Java 同时使用application/json和application/x-www-form-urlencoded,java,jersey,dropwizard,Java,Jersey,Dropwizard,我想要像这样的东西 @POST @Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_FORM_URLENCODED}) @Path("/") void create(@Suspended final AsyncResponse asyncResponse, @ApiParam(required = true) @NotNull @Valid final CreateServiceRequest serv

我想要像这样的东西

@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编码。但是当我用
-dfoo=bar
发出POST请求时,我得到415个不支持的格式化错误


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

我认为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)
}

对于application/x-ww-form-urlencoded方法,您需要使用
@BeanParam