There's a recent thread titled, "Adding grammar to the resources" which discusses a similar use case. As Jakub mentioned in that thread you could use @UriInfo.getQueryParameters() & .getPathParameters() (assuming the ID is a path param) in on PUT endpoint.
You would want your endpoint to look like:
@PUT
@Path("/admin/pojo1s/{id}")
@Produces(MediaType.WHATEVER)
public Response updatePojo(@UriInfo uriInfo) {...}
I use the plural of your resource name in the path. This allows for getting all pojo1s w/GET /admin/pojo1s/ or a specific one with GET /admin/pojo1s/{id}.
It also allows for creating one or more with a POST /admin/pojo1s
Another option would to use a DTO or JAXB bean to marshall a JSON encoded request into and then process the fields. The advantage there is you now have type information in the endpoint signature which you can use to aid in dynamically validating the data and constructing the update statement (esp. useful if you leverage JPA). So for example you could do something like:
@PUT
@Path("/admin/pojos1/{id}")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.WHATEVER)
public Response updatePojo(@PathParam("id") final long pojo1Id, final Pojo1 pojo1) { ... }
The PUT request would contain a JSON representation of the pojo1 object with the field values to be updated. If you use JAXB for your JSON then Pojo1 would be your XmlRootElement class and registered with the JAXBContextResolver (and it could also be your JPA entity bean if you are using that). You could also you Jackson POJO support too. In any case then you can just take the pojo1 and process the fields for updating and you can get as fancy as you want with that process but you only have one endpoint.
What you don't want to do is to have your Path look like "/admin/pojo1/12345/field1/newvalue" as Path's point to resources not values.
-Noah
On Aug 7, 2013, at 1:55 PM, Richard Sand <rsand_at_idfconnect.com> wrote:
> Hi all - quick question about the best way to create a single resource for supporting CRUD operations on several different POJOs.
>
> I have two POJOs which represent two tables in a database. Each POJO has getters and setters for the fields in each table. Each has an "ID" field which is a number for the primary key.
>
> I have a resource class which initializes the database connections and controls the POJOs. All of the operations take the ID field on the URI. So a request for:
>
> GET /admin/pojo1/12345
>
> will return POJO1 with id=12345 as a JSon object - this works fine. My Admin class has a method called getPojo1 with @Path("pojo1/{id: [0-9]*}")
>
> Now I want to support manipulating individual fields as Strings, like:
>
> GET /admin/pojo1/12345/field1
> PUT /admin/pojo1/12345/field1/newvalue
>
> Right now, this means adding methods for each individual field for each POJO into my Admin class. So my simple question is - is there better dispatch pattern I can use in my Admin class that doesn't require a separate method for each POJO x Field x Method? 5 POJOs times 15 fields per POJO times 4 methods = 300 methods = ugly!
>
> Thanks for any advice!
>
> Best regards,
>
> Richard
>