I would use the Hibernate Validator (JSR 303 or whatever you want to
call it) and write a custom validator if needed. Then just annotate
your domain classes. There is a built in one for email but you'll
need to build one for ZIP or telephone number.
http://docs.jboss.org/hibernate/stable/validator/reference/en/html_single/
Have to call the validate step yourself but it makes it really easy to
generalize the validation.
Chris
On Wed, Apr 14, 2010 at 2:50 PM, Dan Forward <dan-nabble_at_forwardhome.com> wrote:
> 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.
> 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: Custom Invalid Parameter Handling
> Sent from the Jersey mailing list archive at Nabble.com.
>