fastjson测试版本:1.2.76 jdk版本:OracleJDK 16.0.1 测试代码:
class LocalDateTimeTest {
@Data
private class TestModel {
private LocalDateTime createTime;
}
@Test
void test() throws Exception {
TestModel model = new TestModel();
model.setCreateTime(LocalDateTime.now());
FastJsonConfig config = new FastJsonConfig();
config.setDateFormat("不论填写什么值都一样,哪怕就是这几个汉字");
FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
converter.setFastJsonConfig(config);
converter.canRead(TestModel.class, MediaType.APPLICATION_JSON_UTF8);
converter.canWrite(TestModel.class, MediaType.APPLICATION_JSON_UTF8);
final ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
HttpOutputMessage out = new HttpOutputMessage() {
public HttpHeaders getHeaders() {
return new HttpHeaders() {
private static final long serialVersionUID = 1L;
@Override
public MediaType getContentType() {
return MediaType.APPLICATION_JSON;
}
};
}
public OutputStream getBody() throws IOException {
return byteOut;
}
};
converter.write(model, TestModel.class, MediaType.APPLICATION_JSON_UTF8, out);
byte[] bytes = byteOut.toByteArray();
String jsonString = new String(bytes, "UTF-8");
System.out.println(jsonString);
}
}
输出结果:
{"createTime":"2021-06-13 16:08:20"}