You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomee.apache.org by jllado <jl...@gmail.com> on 2016/04/21 11:27:21 UTC

JAX RS LocalDateParameterConverter and WebApplicationException for invalid date format

Hi,

I would like to throw a WebApplicationException (with a proper response
error) when the date has an incorrect format. Something like this:
public class LocalDateParameterConverter implements
ParamConverter<LocalDate> {

    public static final String FORMAT = "yyyyMMdd"; // set the format to
whatever you need

    @Override
    public LocalDate fromString(String value) {
        try {
            if (value == null) {
                return null;
            }
            return LocalDate.parse(value,
DateTimeFormatter.ofPattern(FORMAT));
        } catch (DateTimeParseException e) { //NOSONAR
            throw new
WebApplicationException(Response.status(Response.Status.BAD_REQUEST).entity(new
ErrorRS("The date format is invalid. Please, it has to be " + 
FORMAT)).build());
        }
    }

    @Override
    public String toString(LocalDate value) {
        if (value == null) {
            return null;
        }
        return value.format(DateTimeFormatter.ofPattern(FORMAT));
    }

}

But the MediaType, of the @Produces method, is not respected... :S

Any idea of how to do it?

Thanks,
Juan Lladó



--
View this message in context: http://tomee-openejb.979440.n4.nabble.com/JAX-RS-LocalDateParameterConverter-and-WebApplicationException-for-invalid-date-format-tp4678181.html
Sent from the TomEE Users mailing list archive at Nabble.com.

Re: JAX RS LocalDateParameterConverter and WebApplicationException for invalid date format

Posted by jllado <jl...@gmail.com>.
Ufff :P

I think I'll just throw the response with MediaType.TEXT_PLAIN...

Thanks!



--
View this message in context: http://tomee-openejb.979440.n4.nabble.com/JAX-RS-LocalDateParameterConverter-and-WebApplicationException-for-invalid-date-format-tp4678181p4678186.html
Sent from the TomEE Users mailing list archive at Nabble.com.

Re: JAX RS LocalDateParameterConverter and WebApplicationException for invalid date format

Posted by Romain Manni-Bucau <rm...@gmail.com>.
I fear you will need to use something else than a parameter converter which
can throw an IllegalArgumentException which is then automatically converted
(
https://github.com/apache/cxf/blob/ac9b9b1898bbe0cc911b34cc3d32664ac59fdc34/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/InjectionUtils.java#L460
)

A workaround can be to return null and use an interceptor you add through
an extension (to not need to decorate method manually) which does this
logic. The param converter can still be there to ensure types are matching
but it would return a mock (constant for perf reasons) which will get
replaced by the interceptor later.


Romain Manni-Bucau
@rmannibucau <https://twitter.com/rmannibucau> |  Blog
<http://rmannibucau.wordpress.com> | Github <https://github.com/rmannibucau> |
LinkedIn <https://www.linkedin.com/in/rmannibucau> | Tomitriber
<http://www.tomitribe.com> | JavaEE Factory
<https://javaeefactory-rmannibucau.rhcloud.com>

2016-04-21 11:27 GMT+02:00 jllado <jl...@gmail.com>:

> Hi,
>
> I would like to throw a WebApplicationException (with a proper response
> error) when the date has an incorrect format. Something like this:
> public class LocalDateParameterConverter implements
> ParamConverter<LocalDate> {
>
>     public static final String FORMAT = "yyyyMMdd"; // set the format to
> whatever you need
>
>     @Override
>     public LocalDate fromString(String value) {
>         try {
>             if (value == null) {
>                 return null;
>             }
>             return LocalDate.parse(value,
> DateTimeFormatter.ofPattern(FORMAT));
>         } catch (DateTimeParseException e) { //NOSONAR
>             throw new
>
> WebApplicationException(Response.status(Response.Status.BAD_REQUEST).entity(new
> ErrorRS("The date format is invalid. Please, it has to be " +
> FORMAT)).build());
>         }
>     }
>
>     @Override
>     public String toString(LocalDate value) {
>         if (value == null) {
>             return null;
>         }
>         return value.format(DateTimeFormatter.ofPattern(FORMAT));
>     }
>
> }
>
> But the MediaType, of the @Produces method, is not respected... :S
>
> Any idea of how to do it?
>
> Thanks,
> Juan Lladó
>
>
>
> --
> View this message in context:
> http://tomee-openejb.979440.n4.nabble.com/JAX-RS-LocalDateParameterConverter-and-WebApplicationException-for-invalid-date-format-tp4678181.html
> Sent from the TomEE Users mailing list archive at Nabble.com.
>