【问题标题】:Stange RESTful Java EE behaviourStange RESTful Java EE 行为
【发布时间】:2015-10-05 21:38:18
【问题描述】:

我正在 Glassfish 4.1 上制作 Java EE 7 应用程序(战争)。
该应用程序是一个简单的 CRUD 应用程序。
为了测试 CRUD 操作,我制作了一个 Java SE 客户端(使用 Jersey2 客户端 api)。 奇怪的行为是 HTTP POST 创建没有检测到对象的一个​​字段:
我有一个“birthDate”属性,它总是在服务器端出现“null”。
我不明白为什么会发生这种情况以及如何解决?

这是我在服务器端使用的依赖项:

    <dependency>
        <groupId>org.eclipse.persistence</groupId>
        <artifactId>org.eclipse.persistence.core</artifactId>
        <version>2.6.0</version>
        <type>jar</type>
    </dependency>
    <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-web-api</artifactId>
        <version>7.0</version>
        <scope>provided</scope>
    </dependency>

这是客户端依赖项:

    <dependency>
        <groupId>org.glassfish.jersey.core</groupId>
        <artifactId>jersey-client</artifactId>
        <version>2.22</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.jaxrs</groupId>
        <artifactId>jackson-jaxrs-json-provider</artifactId>
        <version>2.6.2</version>
    </dependency>

这是客户端的创建块:

    Client client = ClientBuilder.newClient();
    WebTarget webTarget = client.target("http://localhost:8080/MedicalCenter/rest/customers");

    Customer customer = new Customer("Jason", "Bourne", new Date(), "This is description", new ArrayList<Device>());

    ObjectMapper mapper = new ObjectMapper();

    String mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
    result = mapper.writeValueAsString(customer);

    Response response = webTarget.request("application/json")
            .post(Entity.entity(result, MediaType.APPLICATION_JSON_TYPE));

这是一个发送的 JSON 示例:

Here is an example {"id":null,
"name":"Jason",
"familyName":"Bourne",
"birthDate":"2015-10-05T21:41:43‌​.044+0000",
"description":"This is description",
"creation":"2015-10-05T21:41:43.044+0000",
"enabled":true,
"system":t‌​rue,
"version":null,
"devices":[]}

请帮帮我:) 谢谢!!

【问题讨论】:

  • 你打印出 JSON 的样子了吗?数据序列化不会像服务器期望的那样出来。
  • 检查以确保没有任何东西是瞬态的
  • 这是什么String mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
  • 向我们展示 Customer 类。
  • @MarquisBlount:我没有任何 Transient 字段,我的实体用 @XmlRootElement 和 @XmlAccessorType(XmlAccessType.FIELD) 注释

标签: java json rest jersey jersey-2.0


【解决方案1】:

当使用 Jersey 1.19 客户端捆绑包创建 http 帖子时,情况变得更加奇怪。
所以我得到了一个确认,说这是服务器端的反序列化问题的假设是不正确的,因为它适用于 jersey 1.19 客户端包。
我重新开始排查,对比了 Jersey 1 和 Jersey 2 客户端的 JSON 输出,发现不同的是birthDate的DateFormat:

  • 杰西 2:“birthDate”:“1990-05-04T22:00:00.000+0000”
  • 球衣 1: "birthDate":"2015-10-06T00:24:32.693+02:00"

所以我尝试在 Jersey 2 客户端中强制使用 DateFormat:

DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
//Jackson Object Mapper
ObjectMapper mapper = new ObjectMapper();
mapper.setDateFormat(sdf);

这是 5/5

调用此解决方法的下一个问题是,为什么唯一接受的格式是这个?

希望此解决方法能帮助遇到相同问题的每个人!

【讨论】:

  • 要考虑的一件事是,Glassfish 中使用的实际默认 JSON 提供程序是 MOXy 而不是 Jackson,如 here 所述。
猜你喜欢
  • 2012-03-13
  • 2015-03-08
  • 1970-01-01
  • 1970-01-01
  • 2017-01-16
  • 1970-01-01
  • 2015-04-29
  • 1970-01-01
  • 2015-12-08
相关资源
最近更新 更多