6

JSON を Date オブジェクトに解析する際に問題があります。

私はJersey 1.14、Tomcat 7を使用しています。

私のリソースクラス:

    @Path("/user")
    @Produces(MediaType.APPLICATION_JSON)
    public class UserResource {
        @Path("/~/update")
        @POST
        @Consumes(MediaType.APPLICATION_JSON)
        public Response updateUser(User user) {
            // There are update in DB
            // but Date field always null
            return Response.ok().build();
        }
        @Path("/~/get")
        @GET
        public User getUser() {
            // There are I fetch User from DB
            // Works fine, User instance returns in correct JSON
            return user;
        }

    }

私のモデル:

    @XmlAccessorType(XmlAccessType.NONE)
    @XmlRootElement(name = "user")
    public class User {
        @XmlElement(name = "name")
        private String name;
        @XmlElement(name = "myDate")
        @XmlJavaTypeAdapter(DateAdapter.class)
        private Date myDate;
        // Getters and Setters
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public Date getMyDate() {
            return myDate;
        }
        public void setMyDate(Date myDate) {
            this.myDate = myDate;
        }
    }

Dateクラスの私のXmlAdapter:

    public class DateAdapter extends XmlAdapter<String, Date> {

        private static final SimpleDateFormat dateFormat = new SimpleDateFormat(
        "yyyy-MM-dd");

        @Override
        public String marshal(Date v) {
            return dateFormat.format(v);
        }

        @Override
        public Date unmarshal(String v) {
            try {
                return dateFormat.parse(v);
            } catch (ParseException e) {
                throw new WebApplicationException();
            }
        }
    }

POST メソッドで常に null Date フィールドを使用するのはなぜですか? 送信しようとしているこの JSON:

{"name":"Peter","myDate":"1988-05-31"}

PS 下手な英語で申し訳ありません。

アップデート。 私のクライアントコード:

    public class Tester {

        public static void main(String... args) throws FileNotFoundException, JSONException {

            Client c = Client.create();
            WebResource r = c.resource("http://localhost:8080/myapp/user/~/get");
            ClientResponse resp = r.get(ClientResponse.class);
            JSONObject entity = resp.getEntity(JSONObject.class);
            entity.remove("myDate");
            entity.put("myDate", "1988-05-31");
            r = c.resource("http://localhost:8080/myapp/user/~/update");
            resp = r.entity(entity, MediaType.APPLICATION_JSON).post(ClientResponse.class);
            entity = resp.getEntity(JSONObject.class);
            System.out.println(entity);
        }
    }     

更新 2。

アプリで非常にばかげた間違いを見つけました。代わりにjava.util.Date私は Model クラスでjava.sql.Dateを使用しました :( その
ため、Date フィールドは常に null でした。

4

2 に答える 2

2

あなたのサーバーコードは正しいです。エラーはクライアント コードにあります。

resp = r.entity(entity, MediaType.APPLICATION_JSON).post(ClientResponse.class);

次のようにする必要があります。

resp = r.type(MediaType.APPLICATION_JSON).post(ClientResponse.class,entity);
于 2012-11-29T07:55:00.230 に答える
-2

日付をシリアル化/逆シリアル化するか、日付文字列を作成する必要があります。

private String myDate;
于 2012-11-28T18:26:17.767 に答える