我在 glassfish 4 上使用 jersey 2.0 (jax-rs 2.0) 和 jackson 实现了一个安静的 web 服务(我也在 tomcat 8 中尝试了它,结果相同)。我有一个带有日期属性的 dto,我想将它发送到 web 服务。
对于序列化和反序列化,我有自己的 (De)Serializer。在 junit 测试中,我将 bean 发送到 webservice,webservice 将相同的 bean 发送回来。
所以在请求中我看到日期的格式是这样yyyy-MM-dd'T'HH:mm:ss.SSSZ
的,但是在响应中日期就像一个 unixtimestamp 并且我的解串器抛出一个异常java.text.ParseException: Unparseable date: "1378980107682"
为什么我的服务器不使用我的 (De)Serializer 类?
请求:
1 > Content-Type: application/json
{"name":"name1","lastname":"lastname2","date":"2013-09-12T13:26:30.752+0200"}
响应:
2 < X-Powered-By: Servlet/3.1 JSP/2.3 (GlassFish Server Open Source Edition 4.0 Java/Oracle Corporation/1.7)
{"name":"name1","lastname":"lastname2","date":1378985190752}
我的豆子
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
@JsonAutoDetect
@JsonIgnoreProperties (ignoreUnknown = true)
@JsonInclude (Include.NON_NULL)
public class User {
private String name;
private String lastname;
private Date date;
@JsonSerialize (using = MyDateSerializer.class)
public Date getDate() {
return date;
}
@JsonDeserialize (using = MyDateDeserializer.class)
public void setDate(Date date) {
this.date = date;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
}
我的序列化器
public class MyDateSerializer extends JsonSerializer<Date> {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
@Override
public void serialize(Date aDate, JsonGenerator aJsonGenerator, SerializerProvider aSerializerProvider)
throws IOException, JsonProcessingException {
System.out.println("MyDateSerializer.serialize()");
String dateString = dateFormat.format(aDate);
System.out.println("dateString [" + dateString + "]");
aJsonGenerator.writeString(dateString);
}
}
我的反序列化器
public class MyDateDeserializer extends JsonDeserializer<Date> {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
@Override
public Date deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
System.out.println("MyDateDeserializer.deserialize()");
String date = jsonParser.getText();
System.out.println("MyDateDeserializer.getText [" + date + "]");
try {
return format.parse(date);
} catch (ParseException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
}
我的应用程序
public class MyRESTExampleApplication extends ResourceConfig {
public MyRESTExampleApplication() {
packages("com.carano.fleet4all.restExample");
register(JacksonConfigurator.class);
register(JacksonFeature.class);
}
}
我的 web.xml
<display-name>restExample</display-name>
<servlet>
<display-name>JAX-RS REST Servlet</display-name>
<servlet-name>REST-Servlet</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>com.carano.fleet4all.restExample.MyRESTExampleApplication</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>REST-Servlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>