SpringBoot uses Jackson as the default JSON parsing library by default, and if you’re not comfortable with it, you can easily replace it with your favorite JSON library.

However, I still use Jackson personally, one is to reduce dependence, and the other is the official approval of Spring. Other JSON libraries may perform better, but many are not as good to use as Jackson.

Source:

  • Springboot uses fastJson as a JSON parsing framework _liuyinfei_java

Thanks for sharing

configuration

While it’s possible to remove all default converters by converters.clear(), don’t go overboard or non-application/JSON requests won’t be parsed.

@Slf4j
@Configuration
public class AppWebMvcConfig implements WebMvcConfigurer {

    @Override
    public void extendMessageConverters(List
       
        > converters)
       > {
        /* Remove the default Jackson Converter */
        converters.removeIf(converter -> converter instanceof MappingJackson2HttpMessageConverter);
        log.info("Uninstall Jackson parser and add FastJson parser for JSON processing");

        FastJsonHttpMessageConverter fastJsonConverter = new FastJsonHttpMessageConverter();

        /* Valid request type */
        List<MediaType> mediaTypeList = Arrays.asList(
                MediaType.APPLICATION_JSON,
                MediaType.TEXT_HTML,
                MediaType.APPLICATION_XML,
                MediaType.TEXT_XML,
                MediaType.APPLICATION_ATOM_XML,
                MediaType.APPLICATION_PROBLEM_XML,
                MediaType.APPLICATION_XHTML_XML,
                MediaType.APPLICATION_RSS_XML);
        fastJsonConverter.setSupportedMediaTypes(mediaTypeList);

        /* FastJson configuration */
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setCharset(StandardCharsets.UTF_8);
        fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss.SSS");

        fastJsonConfig.setSerializerFeatures(
                SerializerFeature.PrettyFormat,
                SerializerFeature.WriteMapNullValue,
                SerializerFeature.WriteNullListAsEmpty,
                SerializerFeature.WriteDateUseDateFormat,
                SerializerFeature.WriteEnumUsingToString
        );

        SerializeConfig serializeConfig = SerializeConfig.getGlobalInstance();
        /* Big numbers to String */serializeConfig.put(BigInteger.class, ToStringSerializer.instance); serializeConfig.put(Long.class, ToStringSerializer.instance); serializeConfig.put(Long.TYPE, ToStringSerializer.instance); fastJsonConfig.setSerializeConfig(serializeConfig); converters.add(fastJsonConverter); }}Copy the code

validation

Return a JSON response with a timestamp and set the timestamp format to YYYY-MM-DD HH: MM :ss.SSS. If the timestamp format is returned, the configuration is successful.

@Data
@Builder
@AllArgsConstructor
public class Result {
    private Integer code;
    private String msg;
    @JSONField(format = "yyyy-MM-dd HH:mm:ss.SSS")
    private Date timestamp;
    private Object data;
}
Copy the code

Response body:

{
    "code": 0."data": {},
    "msg": "success"."timestamp": "The 2021-08-24 21:42:38. 885"
}
Copy the code

The setup works, but this shows one of the drawbacks of FastJSON, which is that you have to enforce the order of the fields, otherwise the returned fields will be unordered.