4

I have been googling to figure out how I can customize the Date format when I use jax-rs on apache CXF. I looked at the codes, and it seems that it only support primitives, enum and a special hack that assume the type associated with @FormParam has a constructor with a single string parameter. This force me to use String instead of Date if I want to use FormParam. it is kind of ugly. Is there a better way to do it?

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

Thanks

Donal Fellows
  • 133,037
  • 18
  • 149
  • 215
Oscar Chan
  • 1,552
  • 10
  • 13

4 Answers4

4

starting from CXF 2.3.2 registering ParameterHandler will do it. It is also always possible to override the date value (passed as part of the query, etc) using RequestHandler filters for default Date(String) to work

4

One simple apporach is take parameter as String and parse it in method body to convert it to java.util.Date

Another is create one class having constructor takes on parameter of type String. Perform same thing as I told in first approach.

here is the code for second approach.

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

Hope this helps.

Amit Patel
  • 15,609
  • 18
  • 68
  • 106
0

In Apache-cxf 3.0, you can use a ParamConverterProvider to convert a parameter to a Date.

The following code is copied from 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);
    }

}
Community
  • 1
  • 1
Christophe Weis
  • 2,518
  • 4
  • 28
  • 32
0

After reading the CXF codes (2.2.5), it is not possible, and it is hardcoded to use the Date(String) constructor, so whatever Date(String) support.

Oscar Chan
  • 1,552
  • 10
  • 13