返回给前端的json,long转string

返回给前端的json,long转stringpackagecom.zhidianfan.ydusermanager.config;importcom.fasterxml.jackson.databind.ObjectMapper;importcom.fasterxml.jackson.databind.module.SimpleModule;importcom.fasterxml.jackson.databind.ser.s…

大家好,又见面了,我是你们的朋友全栈君。

package com.zhidianfan.ydusermanager.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.web.PageableHandlerMethodArgumentResolver;
import org.springframework.http.converter.ByteArrayHttpMessageConverter;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.ResourceHttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import java.util.List;

/** * @Author sherry * @Description * @Date Create in 2019-03-15 * @Modified By: */
@Configuration
public class WebConfig extends WebMvcConfigurationSupport { 
   

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { 
   
        converters.add(new ByteArrayHttpMessageConverter());
        converters.add(new StringHttpMessageConverter());
        converters.add(new ResourceHttpMessageConverter());
        converters.add(new AllEncompassingFormHttpMessageConverter());
        converters.add(new StringHttpMessageConverter());
        converters.add(longToStringConverter());
    }

    /** * 将返回给前端的Long和long,统一转化成字符串 * * @return */
    @Bean
    public MappingJackson2HttpMessageConverter longToStringConverter() { 
   
        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
        ObjectMapper mapper = new ObjectMapper();
        SimpleModule simpleModule = new SimpleModule();
        simpleModule.addSerializer(Long.class, ToStringSerializer.instance);//Long
        simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance);//long
        mapper.registerModule(simpleModule);
        converter.setObjectMapper(mapper);
        return converter;
    }

    /** * 必须加上,否则Controller中的分页参数对象无法实例化 * * @param argumentResolvers */
    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) { 
   
        // 注册Spring data jpa pageable的参数分解器
        argumentResolvers.add(new PageableHandlerMethodArgumentResolver());
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/146585.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • eplan用一段时间就要激活码【中文破解版】2022.01.20

    (eplan用一段时间就要激活码)2021最新分享一个能用的的激活码出来,希望能帮到需要激活的朋友。目前这个是能用的,但是用的人多了之后也会失效,会不定时更新的,大家持续关注此网站~https://javaforall.net/100143.htmlIntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,上面是详细链接哦~0H…

    2022年3月31日
    182
  • nginx设置编码格式utf-8

    nginx设置编码格式utf-8nginx设置编码格式utf-8在server下配置charsetutf-8;server{listen8000;server_namelocalhost;charsetutf-8;}后台使用tomcat时,get请求参数乱码更改nginx编码格式设置无效需要更改tomcat编…

    2022年7月11日
    23
  • Python框架区别是什么?比较常用的框架有哪些?

    Python框架区别是什么?比较常用的框架有哪些?前言本文的文字及图片来源于网络,仅供学习、交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理。众所周知,Python开发框架大大减少了开发者不必要的重复劳动,提高了项目开发效率的同时,还使得创建的程序更加稳定。目前比较主流的Python框架都有哪些呢?一般大家用的比较多的是Django、Flask、Scrapy、Diesel、Cubes、Pulsar和Tornado。那么这些Python框架的区别是什么呢?下面我们一起来看看这些Python框架的不同适用环境。.

    2022年5月31日
    32
  • Supervisor进程管理详解「建议收藏」

    Supervisor进程管理详解「建议收藏」文章目录1.supervisor简介2.supervisor安装2.1安装方式2.2验证3.supervisor配置文件3.1主配置文件3.2子配置文件(program配置)3.2.1详细配置3.2.2公司配置4.进程管理命令5.web管理(不常用)6.Supervisor配置systemctl服务7.Supervisor管理redis和nginx7.1安装nginx和redis7.2配置文件7.3重新加载新配置文件7.4测试8.组管理8.1部署三个redis8.2

    2025年9月1日
    5
  • esp-idf的内存管理——tlsf算法

    esp-idf的内存管理——tlsf算法目录1最初还不是tlsf2为什么要引入tlsf3idf中使用的tlsf算法的设计与实现4源码走读参考1最初还不是tlsf2为什么要引入tlsf3idf中使用的tlsf算法的设计与实现4源码走读参考[1]半文钱的博客[2]upstream所在的github地址注意事项放到内存调试去说:用户需要关注的:内存的硬件特性(caps)内存的访问速度内存是否支持原子操作内存是否可以由CPU直接访问用户在使用时:用户自己也要对自己的应用需要使用的内存做一些安排,有的内存比

    2022年6月29日
    25
  • 深入koa2源码

    深入koa2源码

    2021年6月19日
    113

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号