GWTP icon indicating copy to clipboard operation
GWTP copied to clipboard

Serialize bean-like objects to url paramaters automatically

Open spg opened this issue 11 years ago • 6 comments

Suppose I flush a bean from an GWT Editor, and that bean represents search parameters:

class SearchParams {
    String name;
    int age;
    State state;
    String zip;
}

A common REST practice is to use a GET request and pass the search as query params:

[GET] /search?name=Bob&age=20&state=OH&zip=90210

Right now, the only way to build this query string is this is to decompose the POJO into the service method:

@Path("search")
public interface UsersService {
    @GET
    RestAction<List<User>> searchUsers(@QueryParam("name") String name, @QueryParam("age") int age, @QueryParam("state") State state, @QueryParam("zip") String zip);

It would be awesome to pass the SearchParams object as-is to the service method:

@Path("search")
public interface UsersService {
    @GET
    RestAction<List<User>> searchUsers(@QueryParam("") SearchParams searchParams);

This imposes a few questions that need answers:

  • How do we translate nested objects (e.g. : SearchParams -> Address -> ...) into query params?
  • What annotation should be used in the service method? Is there an already existing JAX-RS annotation that exists for this?

spg avatar Jun 03 '14 15:06 spg

That's a good idea.

Pacane avatar Jun 03 '14 15:06 Pacane

https://jersey.java.net/documentation/latest/jaxrs-resources.html#d0e1889

christiangoudreau avatar Jun 03 '14 18:06 christiangoudreau

+1 good idea, I would recommend that as well

confile avatar Jun 03 '14 18:06 confile

When filtering resources, it's a good practice to place matrix params after them, like /groups;class=a.

It's not the case presented (a search - action - endpoint) but if you're going to developer such feature, you should provide options of uri forming.

reinert avatar Jun 05 '14 15:06 reinert

That's achievable through @BeanParam which is part of Jax-RS 2.0 spec. Let's plan this for 1.4

christiangoudreau avatar Aug 03 '14 20:08 christiangoudreau

So, is there any way to use @BeanParam now?

hacker-cb avatar Feb 24 '17 12:02 hacker-cb