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配置
packagecom.gis.fastjson.config;importcom.alibaba.fastjson2.JSONReader;importcom.alibaba.fastjson2.JSONWriter;importcom.alibaba.fastjson2.support.config.FastJsonConfig;importcom.alibaba.fastjson2.support.spring.http.converter.FastJsonHttpMessageConverter;importorg.springframework.context.annotation.Configuration;importorg.springframework.http.MediaType;importorg.springframework.http.converter.HttpMessageConverter;importorg.springframework.web.servlet.config.annotation.WebMvcConfigurer;importjava.nio.charset.StandardCharsets;importjava.util.Collections;importjava.util.List;@ConfigurationpublicclassWebMvcConfigimplementsWebMvcConfigurer{@OverridepublicvoidconfigureMessageConverters(List<HttpMessageConverter<?>> converters){FastJsonHttpMessageConverter converter =newFastJsonHttpMessageConverter();//custom configurationFastJsonConfig config =newFastJsonConfig();
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测试
packagecom.gis.fastjson.controller;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RestController;importjava.util.HashMap;importjava.util.Map;@RestControllerpublicclassHelloWorldController{@RequestMapping("/test")publicMap<String,Object>showHelloWorld(){Map<String,Object> map =newHashMap<>();
map.put("msg","Hello World!");return map;}}
5、闭坑指南
BigDecimal精度丢失
@TestpublicvoidtoJSONString()throwsParseException{BookDTO book =newBookDTO();BigDecimal money =newBigDecimal(-40090.07d);
money = book.setScale(4,RoundingMode.HALF_UP);
book.setMoney(money);String createtime ="2024-07-22 09:03:26.968";SimpleDateFormat format =newSimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");Date date = format.parse(createtime);
book.setCreateTime(date);List<BookDTO> list =newArrayList<>();
list.add(book);String json =JSON.toJSONString(list);System.out.println(json);// 解决方法// String json = JSON.toJSONString(list, JSONWriter.Feature.WriteBigDecimalAsPlain);}}
日期解析问题
@TestpublicvoidparseArray(){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”)
版权归原作者 gis分享者 所有, 如有侵权,请联系我们删除。