users@jersey.java.net

Custom Invalid Parameter Handling

From: Dan Forward <dan-nabble_at_forwardhome.com>
Date: Wed, 14 Apr 2010 13:50:31 -0800 (PST)

I have several wrapper classes that validate simple strings, for example,
EmailAddress, ZIPCode, and TelephoneNumber. Each has a public static factory
called getInstance(String). A custom RuntimeException is thrown if the input
is not valid for the type. If one is not valid, the client needs to know
which one so the appropriate field may be highlighted. I would like to use a
specially-crafted JSON response wrapped in a 400 Bad Request.


An example method signature would be as follows:



@POST
@Path("create")
@Produces(MediaType.APPLICATION_JSON)
public String createAccount(
        @FormParam("email") final EmailAddress email,
        @FormParam("zipCode") final ZIPCode zipCode,
        @FormParam("phone") final TelephoneNumber phone)


An example response would be something like this:


{ field: "email", message: "Invalid email" }

To that end, I have two questions:


1) How do I have Jersey call the getInstance(String) static factory method?


2) How do I have Jersey include a JSON message if the factory method should
fail?


A work-around for both of these would be to use String values and do all the
work in the method. Another would be to extend each class with a custom
handler as depicted here:
https://jersey.dev.java.net/nonav/documentation/latest/user-guide.html#d4e226
https://jersey.dev.java.net/nonav/documentation/latest/user-guide.html#d4e226
. Neither one is very attractive since it would require extra work for every
method or every class.


What I am looking for is a way to handle these situations generally,
something along the lines of implementing my own
MultivaluedParameterExtractorProvider. I think I could handle the first
scenario with a custom StringReaderProvider, but the exception handling
would have to be somewhere else.


Sincerely,


Dan Forward

-- 
View this message in context: http://n2.nabble.com/Custom-Invalid-Parameter-Handling-tp4904335p4904335.html
Sent from the Jersey mailing list archive at Nabble.com.