使用到的jar包如下:
测试代码如下:
package proxy;
import java.util.Date;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.AllArgsConstructor;
import lombok.Data;
public class Test {
@Data
@AllArgsConstructor
class User {
private String name;
private int age;
private Date date;
}
public static void main(String[] args) {
User user = new Test().new User("jerry", 20, new Date());
String json = "";
ObjectMapper mapper = new ObjectMapper();
try {
json = mapper.writeValueAsString(user);
System.out.println(json);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
}
}
输出结果: