2012-11-28 64 views
4

我有解析JSON到日期对象的问题。泽西JSON和日期

我用的泽西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方法中d始终为空日期字段? 我试图发送的这个JSON:

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

P.S.对不起,我的英语不好。

UPD。 我的客户端代码:

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

UPD 2.

我发现在我的应用程序非常愚蠢的错误。 相反java.util.Date我以前java.sql.Date在我的模型类:(
这就是为什么日期字段始终为空。

+0

你能发布发送JSON的客户端代码吗? – secondflying

+0

谢谢你的问题。我发布了客户端代码。 – chepiov

+0

我遵循代码并写一个演示,但我不工作。看来DateAdapter直接被忽略。我还问了一个问题http://stackoverflow.com/questions/17057150/xmljavatypeadapter-doesnt-work。 – Stony

回答

2

你的服务器的代码是正确的错误是在客户端代码:。

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

应该是:

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

非常感谢。 不幸的是,它不适合我。 这些方法有什么区别? 查看API文档后,我决定在这两种方法中没有区别。 – chepiov

+0

对不起,我不小心测试。我再次在自己的环境中测试你的代码,它运行良好。在updateUser方法中,用户参数是正确的。你可以发布你的web.xml吗? – secondflying

+0

谢谢** secondflying ** – chepiov

-2

您必须序列化/反序列化的日期或你有使日期字符串

private String myDate; 
+0

确切日期对象 – chepiov