七.使用fastJson解析器


1、到入jar包

<!-- 添加fastjson 依賴包. -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.15</version>
        </dependency>

2、在啟動程序main方法中加入

@Bean
    public HttpMessageConverters fastHttpMessageConverters() {
        // 1、需要先定義一個 convert 轉換消息的對象;
        FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
        // 2、添加fastJson 的配置信息,比如:是否要格式化返回的json數據;
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
        fastJsonConfig.setCharset(Charset.forName("UTF8"));
        // 3、在convert中添加配置信息.
        fastConverter.setFastJsonConfig(fastJsonConfig);

        HttpMessageConverter<?> converter = fastConverter;
        return new HttpMessageConverters(converter);
    }

3、測試例子

@JSONField(format="yyyy-MM-dd HH:mm")  主要是處理時間
    private Date createTime;

clipboard

其他:

clipboard

有的時候會碰到亂碼的,在controller類的action方法中解決亂碼問題

@RequestMapping(value="modelJson",produces = "text/html;charset=UTF-8")


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM