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

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

问题描述

我的服务定义如下。

public String getData(@QueryParam(date)date date)

我正在尝试从我的客户端向其传递 java.util.Date 是jaxrs:CXF的客户端,而不是通用HTTP客户端或浏览器。)

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).

我的服务收到的日期为 Thu Mar 01 22:33: HTTP URL中的10 IST 2012 。由于CXF无法使用此String创建 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对象从我的客户端传递给服务?感谢任何帮助。

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有一个接受String的公共构造函数

  2. 参数bean有一个静态 valueOf(String)方法。

  1. The parameter bean has a public constructor that accepts a String
  2. The parameter bean has a static valueOf(String) method.

在您的情况下,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

Option 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

Option 2

处理疯狂的日期格式。选项包括:

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

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

Change your method signature to accept a string. Attempt to construct a Date object out of that and if that fails, use your own custom SimpleDateFormat class to parse it.

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才能在尝试默认的非捆绑逻辑之前评估参数处理程序。

Or finally, you can register a parameter handler for dates. Where its logic is simply the same as mentioned for the other options above. Note that you need to be using at least CXF 2.5.3 in order to have your parameter handler evaluated before it tries the default unbundling logic.

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 - 如何将Date作为QueryParam传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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