EvilOrange
EvilOrange

Reputation: 916

Map several @QueryParam to one custom entity

For example, I have class

class MyQuery {
   Date start;
   Date end;
   ChronoUnit scale; // ChronoUnit is enum
}

I want to declare method like:

@Path("/myreport")
public Response generateReport(@EntityFromQuery MyQuery query) {
   // logic to generate.
}

and I want that this method will be invoked when something is hitting url

/myreport?start=2015-01-01&end=2015-01-31&scale=WEEKS

I'm using Jersey. Is there any internal tool to achieve this? Or I need to write my own MessageBodyReader?

Upvotes: 1

Views: 84

Answers (2)

Xavier Coulon
Xavier Coulon

Reputation: 1600

You can take a look at the @BeanParam annotation that you would use instead of @EntityFromQuery in the Java method of your JAX-RS Resouce.

In your MyQuery class, you just need to annotate the getters with @QueryParam("start"), etc.

See https://jax-rs-spec.java.net/nonav/2.0-SNAPSHOT/apidocs/javax/ws/rs/BeanParam.html

HTH.

Upvotes: 2

azraelAT
azraelAT

Reputation: 762

That comes out of the box with jersey (you might want to check some tutorials first..). It might be easier for you to accept all GET-Parameters of your incoming requests as Strings and convert them later on:

@GET
@Path("/myreport")
@Produces(MediaType.{whatever you want to return})
public Response generateReport(@QueryParam("start") String start, @QueryParam ("end") String end, @QueryParam ("scale") String scale) {
   // convert from string to date and from string to chroneUnit
   //do some logic..
}

Upvotes: 0

Related Questions