美文网首页Java学习之路
Springboot集成fastjson

Springboot集成fastjson

作者: 小鱼东西 | 来源:发表于2018-08-23 14:37 被阅读0次

Springboot要集成fastjson配合注解@ResponseBody实现返回数据格式json解析

首先引入pom依赖

 <!--fastjson-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.46</version>
        </dependency>

然后加入消息转换器,实现json数据解析

    @Bean
    public HttpMessageConverters fastJsonConfigure(){
        FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
        // 中文乱码解决方案
        List<MediaType> mediaTypes = new ArrayList<>();
        mediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
        converter.setSupportedMediaTypes(mediaTypes);
        //日期格式化
        fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");
        converter.setFastJsonConfig(fastJsonConfig);
        return new HttpMessageConverters(converter);
    }

控制层代码(这里是封装了通用result类)

    @GetMapping("/getProvince")
    public Result getProvince()
    {
        Provinces provinces = iUserService.getProvince();
        logger.info("省市--->"+provinces.getProvince()+provinces.getCity());
        return ResultUtil.success(provinces);
    }

返回结果


image.png

相关文章

网友评论

    本文标题:Springboot集成fastjson

    本文链接:https://www.haomeiwen.com/subject/ntzpiftx.html