这是我的DateDeserializer类:
public class DateDeserializer extends StdDeserializer<Date>
{
private SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss");
public DateDeserializer()
{
this(null);
}
public DateDeserializer(Class<?> vc) {
super(vc);
}
@Override
public Date deserialize(JsonParser arg0, DeserializationContext arg1) throws IOException, JsonProcessingException {
String date = arg0.getText();
try
{
return formatter.parse(date);
}
catch(ParseException e)
{
throw new RuntimeException(e);
}
}
}
这就是我在实体中注释Date属性的setter(也出现在getter方法中)的方法:
@JsonDeserialize(using = DateDeserializer.class, as = Date.class)
public void setDate(Date date) {
this.date = date;
}
当我以json格式发送请求时,日期的格式与deserializer方法类似,但是我收到以下错误:
“无法从String值构造java.util.Date的实例”
我应该将反序列化剂注射到某个地方吗? 我不明白我做错了什么。
这是我的web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!-- This web.xml file is not required when using Servlet 3.0 container,
see implementation details http://jersey.java.net/nonav/documentation/latest/jax-rs.html -->
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<!-- Configure ContextLoaderListener to use JavaConfigWebApplicationContext
instead of the default XmlWebApplicationContext -->
<context-param>
<param-name>contextClass</param-name>
<param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</context-param>
<!-- Configuration locations must consist of one or more comma- or space-delimited
fully-qualified @Configuration classes -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>org.sharon.trempiada.resources.ResourcesConfiguration org.sharon.trempiada.services.ServicesConfiguration</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<servlet>
<servlet-name>trempiada</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>org.sharon.trempiada.resources,org.glassfish.jersey.jackson</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>trempiada</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
我的模特课:
@Entity
@Table(name = "TBRIDES")
public class Ride
{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name= "ride_id")
private int id;
@Column
private String source;
@Column
private String destination;
@Column
@JsonDeserialize(using = DateDeserializer.class)
private Date date;
@Column
private Time time;
@Column
private boolean isPermanent;
@Column
@JsonDeserialize(using = DateDeserializer.class)
private Date expirationDate;
@ManyToOne
@JoinColumn(name="driver_id", nullable= false)
private User user;
@Column
private String comments;
public String getSource() {
return source;
}
public void setSource(String source) {
this.source = source;
}
public String getDestination() {
return destination;
}
public void setDestination(String destination) {
this.destination = destination;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public Time getTime() {
return time;
}
public void setTime(Time time) {
this.time = time;
}
public boolean isPermanent() {
return isPermanent;
}
public void setPermanent(boolean isPermanent) {
this.isPermanent = isPermanent;
}
public Date getExpirationDate() {
return expirationDate;
}
public void setExpirationDate(Date expirationDate) {
this.expirationDate = expirationDate;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public String getComments() {
return comments;
}
public void setComments(String comments) {
this.comments = comments;
}
}