CXF JAXRS - 如何将日期作为 QueryParam 传递 [英] CXF JAXRS - How do I pass Date as QueryParam

查看:30
本文介绍了CXF JAXRS - 如何将日期作为 QueryParam 传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个如下定义的服务.

I have a service defined as follows.

public String getData(@QueryParam("date") 日期日期)

我正在尝试从我的客户端(它是 jaxrs: CXF 的客户端,而不是通用 HTTP 客户端或浏览器)传递一个 java.util.Date 给它.

I'm trying to pass a java.util.Date to it from my client (which is jaxrs:client of CXF, not a generic HTTP client or browser).

我的服务在 HTTP URL 中接收的日期为 Thu Mar 01 22:33:10 IST 2012.由于 CXF 将无法使用此字符串创建 Date 对象,因此我的客户端会收到 404 错误.我尝试在服务端使用 ParameterHandler,但我仍然无法成功解析它,因为我不期望任何特定格式的日期.

My service receives the date as Thu Mar 01 22:33:10 IST 2012 in the HTTP URL. Since CXF won't be able to create a Date object using this String, my client receives a 404 error. I tried using a ParameterHandler on the service side, but I still can't parse it successfully because I'm not expecting the date in any specific format.

根据这篇文章,传递 Date 应该是开箱即用的,但我似乎无法让基本案例正常工作.我是否需要做任何事情才能成功地将 Date 对象从我的客户端传递给服务?感谢您的帮助.

As per this post, passing a Date is supposed to work out of the box, but I can't seem to get the basic case working. Am I required to do anything in order to successfully pass a Date object from my client to service? Appreciate any help.

谢谢

推荐答案

问题在于 JAX-RS 规定参数分拆可以通过以下两种方式之一完成:

The problem is that JAX-RS dictates that parameter unbundling be done in one of two ways:

  1. 参数 bean 有一个接受字符串的公共构造函数
  2. 参数 bean 有一个静态的 valueOf(String) 方法.

在您的情况下,Date 是通过其 Date(String) 构造函数解绑的,该构造函数无法处理您的客户端发送的输入格式.您有几个选项可以解决这个问题:

In your case, the Date is being unbundled via its Date(String) constructor, which cannot handle the input format your client is sending. You have a couple options available to remedy this:

选项 1

让您的客户在发送日期之前更改日期格式.这是理想的,但可能是最难实现的!

Get your client to change the format of the date before they send it. This is the ideal, but probably the hardest to accomplish!

选项 2

处理疯狂的日期格式.对此的选项是:

Handle the crazy date format. The options for this are:

更改您的方法签名以接受字符串.尝试从中构造一个 Date 对象,如果失败,请使用您自己的自定义 SimpleDateFormat 类来解析它.

static final DateFormat CRAZY_FORMAT = new SimpleDateFormat("");

public String getData(@QueryParam("date") String dateString) {
    final Date date;
    try {
        date = new Date(dateString); // yes, I know this is a deprecated method
    } catch(Exception e) {
        date = CRAZY_FORMAT.parse(dateString);
    }
}

定义您自己的参数类来执行上述逻辑.给它一个调用逻辑的字符串构造函数或静态 valueOf(String) 方法.以及在所有事情都完成后获取日期的附加方法.

Define your own parameter class that does the logic mentioned above. Give it a string constructor or static valueOf(String) method that invokes the logic. And an additional method to get the Date when all is said and done.

public class DateParameter implements Serializable {
    public static DateParameter valueOf(String dateString) {
        try {
            date = new Date(dateString); // yes, I know this is a deprecated method
        } catch(Exception e) {
            date = CRAZY_FORMAT.parse(dateString);
        }
    }

    private Date date;
    // Constructor, Getters, Setters
}

public String getData(@QueryParam("date") DateParameter dateParam) {
    final Date date = dateParam.getDate();
}

或者最后,您可以为日期注册一个参数处理程序.它的逻辑与上面其他选项中提到的逻辑完全相同.请注意,您至少需要使用 CXF 2.5.3 才能在尝试默认解绑逻辑之前评估您的参数处理程序.

public class DateHandler implements ParameterHandler<Date> {
    public Map fromString(String s) {
        final Date date;
        try {
            date = new Date(dateString); // yes, I know this is a deprecated method
        } catch(Exception e) {
            date = CRAZY_FORMAT.parse(dateString);
        }
    }
}

这篇关于CXF JAXRS - 如何将日期作为 QueryParam 传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆