【问题标题】:How to pass Calendar param as input to a rest service?如何将日历参数作为输入传递给休息服务?
【发布时间】:2012-12-05 04:31:19
【问题描述】:

这是我想要做的:

@POST
@Path("/MyPath")
@Produces("text/xml")
@RolesAllowed({"user"})
public Output myMethod (@QueryParam("itemId") long ItemId, 
                                    @QueryParam("plannedstartdate") Calendar plannedStartDate, 
                                    @QueryParam("plannedreturndate") Calendar plannedReturnDate)

我正在使用 JBoss AS7。据我了解,resteasy 已集成到 JBoss AS7 中。我能够运行简单的休息服务。

我发现的关于过时的唯一文档是在链接中: http://docs.jboss.org/resteasy/2.0.0.GA/userguide/html/StringConverter.html#StringParamUnmarshaller

由于说明不清楚,我无法按照此操作解决问题。 当我尝试创建示例中给出的注释 DateFormat 时,它无法识别 StringParamUnmarshaller。我不知道从哪里得到它。如果resteasy已经集成到JBoss AS7中了,这不应该被识别吗?

我的 pom.xml 有以下依赖:

  <!-- Import the JAX-RS API, we use provided scope as the API is included 
     in JBoss AS 7 -->
  <dependency>
     <groupId>org.jboss.spec.javax.ws.rs</groupId>
     <artifactId>jboss-jaxrs-api_1.1_spec</artifactId>
     <scope>provided</scope>
  </dependency>

调用此方法失败,因为没有发生字符串到日历的转换。我不想传递字符串而不是日历,因为还有其他客户端直接进行 java 调用。谁能帮助我如何将日期传递给 Rest Calls?

谢谢 转向

【问题讨论】:

  • 到底是什么问题?您收到什么错误/异常?
  • 我很抱歉。我刚刚更新了最后一段中的问题。

标签: rest date jax-rs jboss7.x resteasy


【解决方案1】:

此问题已解决。请参阅以下代码。

创建一个注解类CalendarFormat.java

@Retention(RUNTIME)
@StringParameterUnmarshallerBinder(CalendarFormatter.class)
public @interface CalendarFormat {
    String value();
}

添加类CalendarFormatter.java

public class CalendarFormatter implements StringParameterUnmarshaller<Calendar> {
    private SimpleDateFormat formatter;

    public void setAnnotations(Annotation[] annotations) {
        CalendarFormat format = FindAnnotation.findAnnotation(annotations, CalendarFormat.class);
        formatter = new SimpleDateFormat(format.value());
    }

    public Calendar fromString(String str) {
        try {
            Calendar cal = Calendar.getInstance();
            cal.setTime(formatter.parse(str));
            return cal;
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }
}

添加到 POM

<dependency>
    <groupId>org.jboss.resteasy</groupId>
    <artifactId>resteasy-jaxrs</artifactId>
    <version>2.3.3.Final</version>
    <scope>provided</scope>
</dependency>

<dependency>
    <groupId>org.jboss.resteasy</groupId>
    <artifactId>resteasy-multipart-provider</artifactId>
    <version>2.3.4.Final</version>
    <exclusions>
        <exclusion>
            <artifactId>resteasy-jaxrs</artifactId>
            <groupId>org.jboss.resteasy</groupId>
        </exclusion>
    </exclusions>
</dependency>

更改方法签名以使用注解

@POST
@Path("/MyPath")
@Produces("text/xml")
@RolesAllowed({"user"})
public Output myMethod(@QueryParam("itemId") long ItemId,            
        @QueryParam("plannedstartdate") @CalendarFormat("MM-dd-yyyy") Calendar plannedStartDate, 
        @QueryParam("plannedreturndate") @CalendarFormat("MM-dd-yyyy") Calendar plannedReturnDate)

就是这样。

【讨论】:

  • 请注意,这是 RESTeasy 特有的技巧,不符合 JAX-RS。
  • 注意:我在 RESTEasy v2.3.1 中使用这种方法时遇到了线程安全问题。似乎 RESTEasy 在部署期间创建了StringParameterUnmarshaller 实例,将它们与它们的资源方法+参数相关联,并将它们重新用于所有请求。我聘请了ThreadLocal 来解决这个问题。
【解决方案2】:

不能使用java.util.Calendar 作为@QueryParam 的参数,因为它没有接受String 的单参数构造函数。您唯一的选择是引入一个新类QueryCalendar,它将具有单参数构造函数并返回Calendar(或继承它)。

关于谁可以成为论点的更多信息:http://docs.oracle.com/javaee/6/api/javax/ws/rs/QueryParam.html

【讨论】:

    猜你喜欢
    • 2015-06-28
    • 2016-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-14
    • 1970-01-01
    • 2020-11-21
    • 2014-03-10
    相关资源
    最近更新 更多