2

I would like to have my JaxRs resource to take a custom method argument that is built from some parameter in the request.
Something to be used in conjunction with another object created from the body. Something like:

 @Resource
 public class MyResource {
      @Path("/resource")
      public Object resource(MyResourceDTO body, AConfiguration conf){

      }
 }

For which the AConfiguration is created from some headers in the request.

How can I achive it?

I need something like th spring webargumentresovler: http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/bind/support/WebArgumentResolver.html

For my case MyResource is a subresource, the method should work also in this case...

rascio
  • 8,968
  • 19
  • 68
  • 108
  • There is no issue with body carrying an object. Do you want pass a configuration object through your header? – Maleen Abewardana Sep 04 '14 at 10:01
  • I would like to create `AConfiguration` from the HttpRequest, like taking 2 Headers and 1 query parameter, or stuff like this, customizing the creation of the object – rascio Sep 04 '14 at 10:11
  • [My answer](http://stackoverflow.com/a/29028880/3249097) over here pretty much solves your use case. Cheers! – cilf Mar 18 '15 at 14:31

2 Answers2

3

If you add a DTO as parameter of your resource method your JAX-RS runtime will try to convert the body of the request into this type. You can additionally add any of the @xParam parameters like @QueryParam as parameters of your resource method. (The only exception is @FormParam as they are found in the body).

If you want to encapsulate multiple of your Params in one object you can use @BeanParam. Your Configuration class could look like this:

public class Configuration {

    @QueryParam("foo")
    private String foo;

    @HeaderParam("bar")
    private String bar;

    // getters + setters

}

And can be used like this:

@POST
public Response someMethod(Dto dto, @BeanParam Configuration conf) {}
lefloh
  • 10,653
  • 3
  • 28
  • 50
0

You can use something like below. Your conf object have be sent as json from the client. If the parameters in conf object have to change dynamically you have to follow the second approach.

 @Resource
 public class MyResource {

      @POST
      @Consumes("application/json")
      @Path("/resource")
      public Object resource(AConfiguration conf){
      // This method can receive multiple objects here. Currently it receives
      // conf object only as the payload of the post method.  
      }

 }

To change the conf object dynamically, You can send json String.

      public Object resource(String confJson){
      // Collect parameters manually here.
      }

In your pom.xml, you should include,

  <dependency>
        <groupId>org.jboss.resteasy</groupId>
        <artifactId>resteasy-jackson-provider</artifactId>
        <version>2.3.1.GA</version>
  </dependency>

Edit:

You can set a json string as a header param (But, not the best practice.) Or you can set different headers at you will and access them using HttpHeaders. Here is an example.

      public Object resource(@Context HttpHeaders confHeaders){
      // Collect parameters manually.
      String confJson = confHeaders.getRequestHeader("confJson").get(0);
      // cast your `confJson` to `AConfiguration aConf` here.
      // process query params and set them to aConf here.
      }
Maleen Abewardana
  • 13,600
  • 4
  • 36
  • 39
  • I already have a Json sent, the conf should be build from headers – rascio Sep 04 '14 at 12:52
  • @rascio I added another example. Check it out. – Maleen Abewardana Sep 04 '14 at 17:07
  • But with `@Context` annotation can I use a custom object? To create `AConfiguration` I need to do some calculation on some headers and querystring parameters. I would like to pass the calculated object to the resource instead of the raw headers and parameters. – rascio Sep 05 '14 at 10:30
  • @rascio If you want to get the calculated object to the resource, I think you will have to implement a `PreProcessInterceptor` (http://docs.jboss.org/resteasy/docs/1.1.GA/userguide/html/Interceptors.html). Else I will modify my example, so you can process it in the resource. – Maleen Abewardana Sep 05 '14 at 10:49