1

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.

varun3
  • 57
  • 6
  • Check the answer for this question https://stackoverflow.com/questions/36295095/how-to-decorate-all-requests-to-take-a-value-from-header-and-add-it-in-the-body – Aarish Ramesh Sep 24 '18 at 08:05
  • What JAX-RS implementation are you using? – Paul Samsotha Sep 24 '18 at 09:44
  • 1
    If you are using Jersey, you can check out [this post](https://stackoverflow.com/a/52476750/2587435) – Paul Samsotha Sep 24 '18 at 09:55
  • I am using RestEasy implementation. This one is specific to spring, I am using JAX-RS https://stackoverflow.com/questions/36295095/how-to-decorate-all-requests-to-take-a-value-from-header-and-add-it-in-the-body – varun3 Sep 25 '18 at 17:01

0 Answers0