dev@jsr311.java.net

Re: JSR311: taking the POJO injection idea further

From: Bill Burke <bburke_at_redhat.com>
Date: Tue, 19 Feb 2008 14:56:10 -0500

Marc Hadley wrote:
> On Feb 19, 2008, at 1:15 PM, Bill Burke wrote:
>
>> I just showed you an example where IAGNI, so I fail to see your point.
>
> Your example could easily be covered by the existing stuff we have:

Then why do we have @HeaderParam or @QueryParam? or even Providers?
They could be built easily using Httpheaders, UriInfo, and InputStream
respectively. i.e.

//_at_HttpParam("foo")
public String get(@HttpContext HttpHeaders)
{
    int foo = Integer.valueOf(httpHeaders.get("foo"));
}

//_at_QueryParam("foo")
public String get(@HttpContext UriInfo info) {
    int foo = Integer.valueOf(info.getQueryParameters().getFirst("foo"));
}

//provider replacement
public void put(InputStream is) {
    MyPojo pojo = new MyPojo(is);
}


Why do we even need JAX-RS for that matter? We could easily use the
Servlet specification instead?


The answer? JAX-RS is a convenience spec.

>
> GET http://host.com/customerdb/;first=Bill;last=Burke;ssn=444-44-4444
>
> @Path("/customerdb/{customerid}")
> @GET
> public String get(@MatrixParam("first") String firstName,
> @MatrixParam("last") String lastName, ...) {...}
>

Your method declaration starts to get REALLY long. What if I need a few
Pathparam, header params or even cookie params? Gets even longer if I
want @DefaultValues


> or
>
> @Path("/customerdb/{customerid}")
> @GET
> public String get() {
> CustomerPK c = new CustomerPK(uriInfo.getMatrixParameters());
> ...
> }
>

Well, there is no uriInfo.getMatrixParameters() and get() needs an
@HttpContext UriInfo. Let's see what's simpler though:

public String get(@PathParam("customerid") CustomerPK pk) {
}

public class CustomerPK {
    @MatrixParam String first;
    @MatrixParam String last;
    @MatrixParam int ssn;
}

vs.

public String get(@PathParam("customerid") PathSegment segment) {
    CustomerPK pk = new CustomerPK(segment.getMatrixParameters());
}

public class CustomerPK {

    public CustomerPK(MultivaluedMap<String, String> matrix) {
        first = matrix.getFirst("first");
        last = matrix.getFirst("last");
        ssn = Integer.valueOf(matrix.getFirst("ssn"));
    }
}

You're not saving a crazy alot of code here, but you are saving some,
and it looks more readable. These savings can be compounded if you
start reusing these mapped pojos in different methods and resources.

-- 
Bill Burke
JBoss, a division of Red Hat
http://bill.burkecentral.com