Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jersey Joda Time ISO 8601 parameter in urlencoded form

I am using Jersey: 1.17.1 and defined a REST service accepting "application/x-www-form-urlencoded". I would like to accept a parameter "b" in ISO-8601 format and let Jersey map this to a Joda DateTime.

@PUT
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public Response createTask(@FormParam("a") String a, @FormParam("b") DateTime b) {
...

but I am getting this exception

SEVERE: The following errors and warnings have been detected with resource and/or provider classes:
SEVERE: Missing dependency for method public de.ast.mae.rest.util.response.Response de.ast.mae.rest.service.tasks.TasksAdminRestService.createTask(java.lang.String,org.joda.time.DateTime) at parameter at index 6
SEVERE: Missing dependency for method public de.ast.mae.rest.util.response.Response de.ast.mae.rest.service.tasks.TasksAdminRestService.createTask(java.lang.String,org.joda.time.DateTime) at parameter at index 6
SEVERE: Method, public de.ast.mae.rest.util.response.Response de.ast.mae.rest.service.tasks.TasksAdminRestService.createTask(java.lang.String,java.lang.String,org.joda.time.DateTime), annotated with PUT of resource, class de.ast.mae.rest.service.tasks.TasksAdminRestService, is not recognized as valid resource method.
Okt 09, 2013 5:54:41 PM com.sun.jersey.spi.spring.container.servlet.SpringServlet initiate
SEVERE: Exception occurred when intialization
com.sun.jersey.spi.inject.Errors$ErrorMessagesException
at com.sun.jersey.spi.inject.Errors.processErrorMessages(Errors.java:170)
at com.sun.jersey.spi.inject.Errors.postProcess(Errors.java:136)

What do I need to do to make this work?

And the answer is: I first upgraded to JAX-RS 2.0 and then used:

@Provider
public class DateTimeParamConverterProvider implements ParamConverterProvider {

    @Override
    public <T> ParamConverter<T> getConverter(Class<T> type, Type genericType, Annotation[] annotations) {
        if (type.equals(DateTime.class)) {
            return (ParamConverter<T>) new DateTimeParamConverter();
        } else {
            return null;
        }

    }

    private static class DateTimeParamConverter implements ParamConverter<DateTime> {
        @Override
        public DateTime fromString(String value) {
            try {
                return ISODateTimeFormat.dateTimeNoMillis().parseDateTime(value);
            } catch (IllegalArgumentException e) {
                return ISODateTimeFormat.dateTime().parseDateTime(value);
            }
        }

        @Override
        public String toString(DateTime value) {
            return value.toString();
        }

    }
}
like image 326
Ronald Avatar asked Nov 01 '22 13:11

Ronald


1 Answers

You can use next solution:

@PUT
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public Response createTask(@FormParam("a") String a, @FormParam("b") String b) 
{
   final DateTime date = ISODateTimeFormat.basicDate().parse(b);
   // ...

basicDate() has format yyyyMMdd.
Appropriate format to your case you can find here
EDIT
From Jersey Documentation

In general the Java type of the method parameter may:

  1. Be a primitive type;

  2. Have a constructor that accepts a single String argument;

  3. Have a static method named valueOf or fromString that accepts a single String argument (see, for example, Integer.valueOf(String) and java.util.UUID.fromString(String));

  4. Have a registered implementation of javax.ws.rs.ext.ParamConverterProvider JAX-RS extension SPI that returns a javax.ws.rs.ext.ParamConverter instance capable of a "from string" conversion for the type. or

  5. Be List, Set or SortedSet, where T satisfies 2 or 3 above. The resulting collection is read-only.

So, posssible solutions are 2 and 4.\

Create class ISODateAsString and use is as parameter

public class ISODateAsString
{
   DateTime dateTime;
   public DateAsString(String date)
   {
      dateTime= ISODateTimeFormat.basicDate().parse(date);
   }
   //...
}  

Or use ParamConverterProvider and ParamConverter

like image 85
Ilya Avatar answered Nov 08 '22 04:11

Ilya