1.spring配置照样是这样:
<mvc:annotation-driven>
2.JsonUtil可以不用了,但如果要自己从response对象输出json,那么还是可以用,但改成了这样
package com.xxx.utils;
import java.io.IOException;
import java.text.SimpleDateFormat;
import org.springframework.stereotype.Component;
import com.fasterxml.jackson.databind.ObjectMapper;
/**
* json处理工具类
* @author zhangle
*/
@Component
public class JsonUtil {
private static final String DEFAULT_DATE_FORMAT="yyyy-MM-dd HH:mm:ss";
private static final ObjectMapper mapper;
static {
SimpleDateFormat dateFormat = new SimpleDateFormat(DEFAULT_DATE_FORMAT);
mapper = new ObjectMapper();
mapper.setDateFormat(dateFormat);
}
public static String toJson(Object obj) {
try {
return mapper.writeValueAsString(obj);
} catch (Exception e) {
throw new RuntimeException("转换json字符失败!");
}
}
public <t> T toObject(String json,Class<t> clazz) {
try {
return mapper.readValue(json, clazz);
} catch (IOException e) {
throw new RuntimeException("将json字符转换为对象时失败!");
}
}
3.实体类的get方法就需要多一个@JsonFormat的注解配置
@DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
public Date getCreateTime() {
return this.createTime;
}
@DateTimeFormat(pattern="yyyy-MM-dd")
@JsonFormat(pattern="yyyy-MM-dd",timezone = "GMT+8")
public Date getBirthday() {
return this.birthday;
}