I want to encapsulate the resource and header/query params in same class.
class AddCommentRequest {
@HeaderParam("X-Session-Id")
private String sessionId;
@HeaderParam("X-Request-Id")
private String requestId;
// This will be part of POST body.
private Comment comment;
}
@Path("/")
interface CommentResources {
@POST
@Consumes({ "application/json" })
@Produces({ "application/json" })
@Path("/comments")
Response addComment(@BeanParam AddCommentRequest request);
}
I know I can do something like below:
@Path("/")
interface CommentResources {
@POST
@Consumes({ "application/json" })
@Produces({ "application/json" })
@Path("/comments")
Response addComment(@HeaderParam("X-Session-Id") String sessionId, @HeaderParam("X-Request-Id") String requestId, Comment comment);
}
But I don't want to opt for this: 1. It bloats method arguments and also this forces me to update method signature everytime I add a new header/query param. 2. These params are common for all APIs and I don't want to repeat myself.
If payload would had been URI encoded I could have used @FormParam but in my case the payload is JSON.