gpt4 book ai didi

urlencode 形式的泽西 Joda Time ISO 8601 参数

转载 作者:行者123 更新时间:2023-12-02 04:18:57 27 4
gpt4 key购买 nike

我正在使用 Jersey: 1.17.1 并定义了一个接受“application/x-www-form-urlencoded”的 REST 服务。我想接受 ISO-8601 格式的参数“b”,并让 Jersey 将其映射到 Joda DateTime。

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

但是我遇到了这个异常

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)

我需要做什么才能使这项工作成功?

答案是:我首先升级到 JAX-RS 2.0,然后使用:

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

}
}

最佳答案

您可以使用下一个解决方案:

@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() 的格式为 yyyyMMdd
您可以找到适合您案例的格式 here
编辑
来自 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.

因此,可能的解决方案是 24。\

创建类ISODateAsString并使用is作为参数

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

或者使用ParamConverterProviderParamConverter

关于urlencode 形式的泽西 Joda Time ISO 8601 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19277306/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com