Jersey Joda Time ISO 8601 parameter in urlencoded form

限于喜欢 提交于 2019-12-10 14:56:34

问题


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();
        }

    }
}

回答1:


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



来源:https://stackoverflow.com/questions/19277306/jersey-joda-time-iso-8601-parameter-in-urlencoded-form

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!