【问题标题】:Custom Date Format with jax-rs in apache cxf?apache cxf中带有jax-rs的自定义日期格式?
【发布时间】:2010-05-14 23:42:56
【问题描述】:

当我在 apache CXF 上使用 jax-rs 时,我一直在谷歌上搜索如何自定义日期格式。我查看了代码,似乎它只支持原语、枚举和一个特殊的 hack,假设与 @FormParam 关联的类型有一个带有单个字符串参数的构造函数。如果我想使用 FormParam,这会迫使我使用 String 而不是 Date。这有点丑。有没有更好的方法?

@POST
@Path("/xxx")
public String addPackage(@FormParam("startDate") Date startDate)
    {
      ...
    } 

谢谢

【问题讨论】:

    标签: cxf jax-rs


    【解决方案1】:

    从 CXF 2.3.2 开始注册 ParameterHandler 就可以了。也总是可以使用 RequestHandler 过滤器覆盖日期值(作为查询的一部分传递等),以使默认 Date(String) 起作用

    【讨论】:

      【解决方案2】:

      一个简单的方法是将参数作为 String 并在方法体中解析它以将其转换为 java.util.Date

      另一种方法是创建一个类,其构造函数采用 String 类型的参数。执行与我在第一种方法中所说的相同的操作。

      这是第二种方法的代码。

      @Path("date-test")
      public class DateTest{
      
          @GET
          @Path("/print-date")
          public void printDate(@FormParam("date") DateAdapter adapter){
              System.out.println(adapter.getDate());
          }
      
          public static class DateAdapter{
              private Date date;
              public DateAdapter(String date){
                  try {
                      this.date = new SimpleDateFormat("dd/MM/yyyy").parse(date);
                  } catch (Exception e) {
      
                  }
              }
      
              public Date getDate(){
                  return this.date;
              }
          }
      }
      

      希望这会有所帮助。

      【讨论】:

        【解决方案3】:

        在阅读了 CXF 代码(2.2.5)之后,这是不可能的,并且它被硬编码为使用 Date(String) 构造函数,所以无论 Date(String) 都支持。

        【讨论】:

          【解决方案4】:

          在 Apache-cxf 3.0 中,您可以使用 ParamConverterProvider 将参数转换为 Date。

          以下代码复制自my answer to this question。

          public class DateParameterConverterProvider implements ParamConverterProvider {
          
              @Override
              public <T> ParamConverter<T> getConverter(Class<T> type, Type type1, Annotation[] antns) {
                  if (Date.class.equals(type)) {
                      return (ParamConverter<T>) new DateParameterConverter();
                  }
                  return null;
              }
          
          }
          
          public class DateParameterConverter implements ParamConverter<Date> {
          
              public static final String format = "yyyy-MM-dd"; // set the format to whatever you need
          
              @Override
              public Date fromString(String string) {
                  SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
                  try {
                      return simpleDateFormat.parse(string);
                  } catch (ParseException ex) {
                      throw new WebApplicationException(ex);
                  }
              }
          
              @Override
              public String toString(Date t) {
                  return new SimpleDateFormat(format).format(t);
              }
          
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2014-05-31
            • 1970-01-01
            • 1970-01-01
            • 2015-10-25
            • 1970-01-01
            • 2013-12-18
            相关资源
            最近更新 更多