使用 RestEasy,如何在传入对象参数之一的属性上使用 @HeaderParam 注释?

Using RestEasy, how can I use a @HeaderParam annotation on the properties of one of the incoming object parameters?

使用 RestEasy,我可以在传入对象参数之一的属性上使用 @HeaderParam@PathParam@QueryParam 注释吗?

这是我的方法:

@Consumes(MediaType.APPLICATION_JSON)
@POST
@Path("/test/{testPathParam}")
@Produces(value = {MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN})
public Response test(
        @HeaderParam("testHeaderParam") String testHeaderParam,
        @QueryParam("testQueryParam") String testQueryParam,
        @PathParam("testPathParam") String testPathParam,
        TestInputBean testInputBean
) {
    Logger.info(this, "testHeaderParam:                    ", testHeaderParam);
    Logger.info(this, "testQueryParam:                     ", testQueryParam);
    Logger.info(this, "testPathParam:                      ", testPathParam);
    Logger.info(this, "testInputBean.getTestHeaderParam(): ", testInputBean.getTestHeaderParam());
    Logger.info(this, "testInputBean.getTestQueryParam():  ", testInputBean.getTestQueryParam());
    Logger.info(this, "testInputBean.getTestPathParam():   ", testInputBean.getTestPathParam());
    Logger.info(this, "testInputBean.anotherParam():       ", testInputBean.anotherParam);
    return null;
}

这是 TestInputBean(删除了 getter 和 setter):

public class TestInputBean {
    @HeaderParam("testHeaderParam")
    String testHeaderParam;
    @QueryParam("testQueryParam")
    String testQueryParam;
    @PathParam("testPathParam")
    String testPathParam;
    String anotherParam;
}

这是请求:

这是输出:

testHeaderParam:                    testHeaderParam-fromHeader
testQueryParam:                     testQueryParam-fromQuery
testPathParam:                      testPathParam-fromPath
testInputBean.getTestHeaderParam(): null
testInputBean.getTestQueryParam():  null
testInputBean.getTestPathParam():   null
testInputBean.anotherParam():       anotherParam-fromJson

但这就是我想要的:

testHeaderParam:                    testHeaderParam-fromHeader
testQueryParam:                     testQueryParam-fromQuery
testPathParam:                      testPathParam-fromPath
testInputBean.getTestHeaderParam(): testHeaderParam-fromHeader
testInputBean.getTestQueryParam():  testQueryParam-fromQuery
testInputBean.getTestPathParam():   testPathParam-fromPath
testInputBean.anotherParam():       anotherParam-fromJson

这可能吗?

您可以使用 @BeanParam 注释将所有 @XxxParam 属性放入一个 bean 中,但我认为不可能将 JSON 实体 bean 也包含在其中这个豆子。您只需要将它作为一个单独的参数。

public class Bean {
    @PathParam("id")
    private String id;
    @XxxParam("..")
    private String param;
    // getters and setters
}

@POST
public Response(@BeanParam Bean bean, JsonBody entity) {}

@BeanParam 是标准的 JAX-RS (2.0) 注释,但 RESTEasy 有一个特定的 @Form 注释,其工作方式与 @BeanParam 完全相同。参见 here for more info. If you are using RESTEasy 2.x, then you can only use @Form,因为 RESTEasy 2.x 仍然使用 JAX-RS 1.x,它没有 @BeanParam.