【发布时间】: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":true,
"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