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

4

4 に答える 4

4

CXF 2.3.2 以降では、ParameterHandler を登録するだけで済みます。また、デフォルトの Date(String) が機能するように RequestHandler フィルターを使用して、日付値 (クエリの一部として渡されるなど) をオーバーライドすることも常に可能です。

于 2011-01-18T16:46:15.593 に答える
4

単純なアプローチの 1 つは、パラメーターを String として受け取り、それをメソッド本体で解析して java.util.Date に変換することです。

もう1つは、コンストラクターがString型のパラメーターを取る1つのクラスを作成することです。最初のアプローチで言ったのと同じことを実行します。

これが2番目のアプローチのコードです。

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

お役に立てれば。

于 2011-05-11T08:08:01.040 に答える
0

CXF コード (2.2.5) を読み取った後では不可能であり、Date(String) コンストラクターを使用するようにハードコーディングされているため、Date(String) がサポートするものは何でも使用できます。

于 2010-09-09T18:01:59.637 に答える