mattalxndr mattalxndr - 7 months ago 28
Java Question

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

Using RestEasy, can I use a

@HeaderParam
,
@PathParam
, or
@QueryParam
annotations on the properties of one of the incoming object parameters?

This is my method:

@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;
}


This is
TestInputBean
(deleted the getters and setters):

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


This is the request:

Request details

This is the output:

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


But this is what I want:

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


Is this possible?

Answer

You can put all the @XxxParam properties into a bean, using the @BeanParam annotation, but I don't think it is possible to also include the JSON entity bean into this bean. You will just need to put is as a separate parameter.

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

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

@BeanParam is standard JAX-RS (2.0) annotation, but RESTEasy has a specific @Form annotation that works exactly the same way as @BeanParam. See here for more info. If you are using RESTEasy 2.x, then you can only use @Form, as RESTEasy 2.x still used JAX-RS 1.x, which doesn't have @BeanParam.

Comments