1、什么是fastjson
fastjson是阿里巴巴开发的一个高性能的Java JSON处理库,它支持将Java对象转换成JSON格式,同时也支持将JSON字符串解析成Java对象。
2、pom.xml 配置
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-json</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.79</version>
</dependency>
3、全局WebMvcConfig配置
package com.gis.fastjson.config;
import com.alibaba.fastjson2.JSONReader;
import com.alibaba.fastjson2.JSONWriter;
import com.alibaba.fastjson2.support.config.FastJsonConfig;
import com.alibaba.fastjson2.support.spring.http.converter.FastJsonHttpMessageConverter;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.List;
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
//custom configuration
FastJsonConfig config = new FastJsonConfig();
config.setDateFormat("yyyy-MM-dd HH:mm:ss");
config.setReaderFeatures(JSONReader.Feature.FieldBased, JSONReader.Feature.SupportArrayToBean);
config.setWriterFeatures(JSONWriter.Feature.WriteMapNullValue, JSONWriter.Feature.PrettyFormat);
converter.setFastJsonConfig(config);
converter.setDefaultCharset(StandardCharsets.UTF_8);
converter.setSupportedMediaTypes(Collections.singletonList(MediaType.APPLICATION_JSON));
converters.add(0, converter);
}
}
4、controller测试
package com.gis.fastjson.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
@RestController
public class HelloWorldController {
@RequestMapping("/test")
public Map<String, Object> showHelloWorld(){
Map<String, Object> map = new HashMap<>();
map.put("msg", "Hello World!");
return map;
}
}
5、闭坑指南
BigDecimal精度丢失
@Test
public void toJSONString() throws ParseException {
BookDTO book = new BookDTO();
BigDecimal money = new BigDecimal(-40090.07d);
money = book.setScale(4, RoundingMode.HALF_UP);
book.setMoney(money);
String createtime ="2024-07-22 09:03:26.968";
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
Date date = format.parse(createtime);
book.setCreateTime(date);
List<BookDTO> list = new ArrayList<>();
list.add(book);
String json =J SON.toJSONString(list);
System.out.println(json);
// 解决方法
// String json = JSON.toJSONString(list, JSONWriter.Feature.WriteBigDecimalAsPlain);
}
}
日期解析问题
@Test
public void parseArray() {
String json="[{\"create_time\":\"2024-07-22 10:03:26.968\",\"money\":-40090.0700}]";
System.out.println(json);
List<BookDTO> list1 = JSON.parseArray(json, BookDTO.class,JSONReader.Feature.SupportSmartMatch);
System.out.println();
}
运行结果
java.time.format.DateTimeParseException: Text ‘2024-07-22 10:03:26.968’ could not be parsed, unparsed text found at index 10
解决方法
BookDTO上加上@JSONField(format= “yyyy-MM-dd HH:mm:ss”)