在SpringMVC中,可以通过在<mvc:annotation-driven>中配置<mvc:message-converters>,把null值统一转换为空字符串,解决这个问题。下面以JSon交互的方式为例说明如何实现:

第一步:创建一个ObjectMapper

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package com.xjj.anes.mvc.converter; 
   
import java.io.IOException; 
   
import com.fasterxml.jackson.core.JsonGenerator; 
import com.fasterxml.jackson.core.JsonProcessingException; 
import com.fasterxml.jackson.databind.JsonSerializer; 
import com.fasterxml.jackson.databind.ObjectMapper; 
import com.fasterxml.jackson.databind.SerializerProvider; 
   
/**
 * @description: 转换null对象为空字符串
 */ 
public class JsonObjectMapper extends ObjectMapper { 
    private static final long serialVersionUID = 1L; 
   
    public JsonObjectMapper() { 
        super(); 
        // 空值处理为空串 
        this.getSerializerProvider().setNullValueSerializer(new JsonSerializer<Object>() { 
            @Override 
            public void serialize(Object value, JsonGenerator jg, SerializerProvider sp) throws IOException, JsonProcessingException { 
                jg.writeString(""); 
            
        }); 
    

  第二步:在SpringMVC配置文件中,把新建的ObjectMapper注入给MappingJackson2HttpMessageConverter

1
2
3
4
5
6
7
8
9
10
<!-- 注册RequestMappingHandlerMapping 和RequestMappingHandlerAdapter 两个bean。--> 
<mvc:annotation-driven> 
    <mvc:message-converters> 
        <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"
            <property name="objectMapper"
                <bean class="com.xjj.anes.mvc.converter.JsonObjectMapper"></bean> 
            </property> 
        </bean> 
    </mvc:message-converters> 
</mvc:annotation-driven> 

SpringMVC统一转换null值为空字符串的方法 !的更多相关文章

  1. SpringMVC统一转换null值为空字符串的方法

    在SpringMVC中,可以通过在<mvc:annotation-driven>中配置<mvc:message-converters>,把null值统一转换为空字符串,解决这个 ...

  2. spring boot 返回json字符串 null值转空字符串

    @Configuration public class JacksonConfig { @Bean @Primary @ConditionalOnMissingBean(ObjectMapper.cl ...

  3. setlocale(LC_ALL, ""); 取值为空字符串" "(注意,不是NULL),则locale与本地环境所使用的编码方式相同(在本地化时,应该很有用);

    在C运行库提供的多字节字符-宽字符转换函数:mbstowcs()/wcstombs()中,需要用到全局变量locale( locale encoding ),以指定多字节字符的编码类型 1. 功能: ...

  4. 3-java中String值为空字符串与null的判断方法

    java中String值为空字符串与null的判断方法 2018年01月21日 14:53:45 阅读数:1189 Java空字符串与null的区别 1.类型 null表示的是一个对象的值,而不是一个 ...

  5. Oracle中的NULL、’’(空字符串)以及’_’(空格)

    本文首发于 http://youngzy.com/ 在Oracle中使用 null,''(空字符串),'_'(空格)时,有没有遇到问题?产生疑惑? null和’’(空字符串)是一个意思 注: 为了便于 ...

  6. Extjs 判断对象是非为null或者为空字符串

    Ext.isEmpty(str,[allowEmptyString]) 如果str为 null undefined a zero-length array a zero-length string ( ...

  7. C#实体类null自动转空字符串

    C#实体类null自动转空字符串 using System.ComponentModel.DataAnnotations; [DisplayFormat(ConvertEmptyStringToNul ...

  8. session在本地可以正常使用,而在sae上却无法使用或者值为空的解决方法

    session在本地可以正常使用,而在sae上却无法使用或者值为空的解决方法: session_start()放在当前页代码的第一行即可解决该问题. 在本地上session_start()如果不是放在 ...

  9. ASP.NET Core WebApi 返回统一格式参数(Json 中 Null 替换为空字符串)

    相关博文:ASP.NET Core WebApi 返回统一格式参数 业务场景: 统一返回格式参数中,如果包含 Null 值,调用方会不太好处理,需要替换为空字符串,示例: { "respon ...

随机推荐

  1. python os.path.dirname()

    ----返回文件所在的路径 ----如果path变量直接是文件名则返回空

  2. 数据迁移工具Sqoop和DataX功能比较

    本文转载自: http://www.cnblogs.com/panfeng412/archive/2013/04/29/data-migration-tool-sqoop-and-datax.html ...

  3. Linux基础命令---pgrep

    pgrep pgrep指令可以按名字或者其他属性搜索指定的进程,显示出进程的id到标准输出. 此命令的适用范围:RedHat.RHEL.Ubuntu.CentOS.SUSE.openSUSE.Fedo ...

  4. bzoj1594 Pku3764 The xor-longest Path

    题目链接 先求每个点到根的异或和 然后就要找出两个点,使dis[a]^dis[b]最大 注意异或的性质,我们可以用trie树,沿着与当前数字每位的相反方向走 #include<algorithm ...

  5. Jmeter压力测试和接口测试

    jmeter是apache公司基于java开发的一款开源压力测试工具,体积小,功能全,使用方便,是一个比较轻量级的测试工具,使用起来非常简单.因为jmeter是java开发的,所以运行的时候必须先要安 ...

  6. python+selenium win32gui实现文件上传 enumerate()

    upload = dr.find_element_by_id('exampleInputFile0') upload.click() time.sleep(1) # win32gui dialog = ...

  7. ARM 架构、ARM7、ARM9、STM32、Cortex M3 M4 、51、AVR 之间有什么区别和联系?(转载自知乎)

    ARM架构:  由英国ARM公司设计的一系列32位的RISC微处理器架构总称,现有ARMv1~ARMv8种类. ARM7:       一类采用ARMv3或ARMv4架构的,使用冯诺依曼结构的内核. ...

  8. linux中没有tree命令,command not found,解决办法

    在有网络的情况下: 1.包管理器安装 centos 中用  yum -y install tree ubuntu 中用  apt-get install tree 当然如果需要权限不要忘了在前面加上 ...

  9. OpenCV常用库函数[典]

    一.core 模块 1.Mat - 基本图像容器 Mat 是一个类,由两个数据部分组成:矩阵头(包含矩阵尺寸,存储方法,存储地址等信息)和一个指向存储所有像素值的矩阵(根据所选存储方法的不同矩阵可以是 ...

  10. Wxpython零基础制作计算器

    本文关于Wxpython零基础利用python3.6在pycharm下制作计算器,文章末尾有免费源代码供下载 以后同步更新到博客园和这个网站,www.empirefree.top, 这个网站备案号没有 ...