打算将数据库取出的Date类型的数据改成一定格式的时间,如改成yyyy-MM-dd。然后在属性的get方法上加了@JsonFormat,结果取出时一直是long类型的Date,代码如下:
package soc.entity;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
import com.fasterxml.jackson.annotation.JsonFormat;
//@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
/*@XmlRootElement
@XmlType(name = "user", propOrder = {
"id",
"name",
"salary",
"birthday"}
)*/
public class User {
private long id;
private String name;
@Min(value=6000,message="最低工资为6000")
@Max(value=25000,message="最高工资为25000")
private Double salary;
private Date birthday;
public User() {
}
public User(long id, String name, Double salary, String birthday) {
super();
this.id = id;
this.name = name;
this.salary = salary;
try {
this.birthday = new SimpleDateFormat("yyyy-MM-dd").parse(birthday);
} catch (ParseException e) {
e.printStackTrace();
}
}
public User(String name, Double salary, String birthday) {
super();
this.name = name;
this.salary = salary;
try {
this.birthday = new SimpleDateFormat("yyyy-MM-dd").parse(birthday);
} catch (ParseException e) {
e.printStackTrace();
}
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getSalary() {
return salary;
}
public void setSalary(Double salary) {
this.salary = salary;
}
// @XmlJavaTypeAdapter(DateAdapter.class)//这种格式也可以<birthday>2018-10-15 10:12:15</birthday>
// @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")//json的返回格式
@JsonFormat(pattern="yyyy/MM/dd HH:mm:ss",timezone = "GMT+8")//json的返回格式
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
@Override
public String toString() {
String birthdayS = (new SimpleDateFormat("yyyy-MM-dd")).format(birthday);
return "User : id="+id+", Name="+name+", Salary="+salary+", Birthday="+birthdayS;
}
}
配置类如下:
package soc.config;
import org.codehaus.jackson.jaxrs.JacksonJsonProvider;
import org.glassfish.jersey.filter.LoggingFilter;
//import org.glassfish.jersey.jackson.JacksonFeature;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.server.spring.scope.RequestContextFilter;
public class ApplicationApi extends ResourceConfig {
public ApplicationApi() {
// 注册数据转换器
register(JacksonJsonProvider.class);
// 注册日志
register(LoggingFilter.class);
// register application resources
// register(UserResource.class);
packages("soc.resource");
// register filters
register(RequestContextFilter.class);
// register mine exception class ,to find the trace of exception
register(DebugMapperException.class);
// register features
// register(JacksonFeature.class);
}
}
经过一番查看,最后发现该配置类里的JacksonJsonProvider是
org.codehaus.jackson.jaxrs.JacksonJsonProvider
而实体User类上的注解@JsonFormat则是
com.fasterxml.jackson.annotation.JsonFormat
两者根本不是同一类型,将JacksonJsonProvider改成
com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider
则问题解决。
综上得出结论,用@JsonFormat注解时,json格式支持的jaxrs的实现类必须与该注解是同一个大类型,即fasterxml,jar包如下:
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-json-provider</artifactId>
<version>2.5.4</version>
</dependency>
如果json格式支持的jaxrs的实现类是codehaus,则@JsonFormat注解不起作用,即不可以是如下jar包:
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-jaxrs</artifactId>
<version>1.9.13</version>
</dependency>