使用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
这可能吗?
答案 0 :(得分:2)
您可以使用@XxxParam
注释将所有@BeanParam
属性放入bean中,但我认为也不可能将JSON实体bean包含到此bean中。您只需要将is作为单独的参数。
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。如果您使用的是RESTEasy 2.x,那么您只能使用@Form
,因为RESTEasy 2.x仍然使用JAX-RS 1.x,它没有@BeanParam
。