随笔-关于SpringBootAPI返回值

写API时返回值取舍总是令人头痛。返回json时,有的接口对接的要求是为null不显示、但是有的接口又是为null必须显示,但是又不能为空、时间类型必须按照一定的格式来编写,每次都要百度一下,痛定思痛,决定好好研究一下,加快编码效率,

1. 值为null时不显示问题

这个东西又分为两种情况
①全局都不显示null
②单独的对象不显示null

1.1 全局都不显示null

这个最简单,在springboot配置文件中加入:

spring.jackson.default-property-inclusion.non_null
1.2 单独的对象不显示null

在对象上加注解:

@JsonInclude(JsonInclude.Include.NON_NULL)//如果字段是null就不返回
public class xxxBean {
	xxx
}

2. 值为null时显示空值

解决方案:在每个属性后面都先赋值为空值

public class xxxBean {
	private Integer field = "";
}

3. 时间返回格式

时间在大多数时候,我们要返回固定的时间格式。

我以前学的是这样的:

public class xxxBean {
	SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
	private Date field;
	public String getField(field){
		return fmt.format(field);
	}
}

但是这次遇到了问题,因为我的Bean用的lombok自动生成get、set、toString,我估计这个无法通过重写get方法实现吧,而且就算可以,每一个都重写一遍感觉也很低端,所以这次换另一个方法解决时间返回值问题

通过写一个继承JsonSerializer类的类,重写serialize方法,本次我以返回时间戳为例:

public class Date2LongSerializer extends JsonSerializer<Date> {

    @Override
    public void serialize(Date date, JsonGenerator gen, SerializerProvider serializers) throws IOException {
        gen.writeNumber(date.getTime() /1000);//这里写需要返回的格式
    }
}

在对象上使用:

@Data
public class xxxBean {

    @JsonSerialize(using = Date2LongSerializer.class)
	private Date field;
	
}

所有用到这个转换的时间类型上加上相同的注解就OK

4. 关于方法过时

当我按照很老的教学项目视频敲代码时,发现以下这个方法已经过时了。

@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)

这个时候不用着急

CTRL+左键进入已过时的方法(include)

	/**
     * Which properties of annotated Bean are
     * to be included in serialization (has no effect on other types
     * like enums, primitives or collections).
     * Choices are "all", "properties that have value other than null"
     * and "properties that have non-default value" (i.e. default value
     * being property setting for a Bean constructed with default no-arg
     * constructor, often null).
     *<p>
     * This property has been replaced by special-purpose {@link com.fasterxml.jackson.annotation.JsonInclude}
     * annotation, introduced in Jackson 2.0.
     *<p>
     * Note that Jackson 2.3 changed default to <code>DEFAULT_INCLUSION</code>,
     * which is roughly same as saying "whatever". This is important because
     * it allows hierarchic default values to be used.
     *
     * @deprecated As of Jackson 2.0, this annotation has been replaced
     *    by {@link com.fasterxml.jackson.annotation.JsonInclude}
     */
    @Deprecated
    public Inclusion include() default Inclusion.DEFAULT_INCLUSION;

发现

This property has been replaced by special-purpose {@link com.fasterxml.jackson.annotation.JsonInclude}

所以新的方法就是@link连接后的方法,这是一般框架编写时都会有的提醒

CTRL+左键点击进入 com.fasterxml.jackson.annotation.JsonInclude

查看源码

protected final static Value EMPTY = new Value(Include.USE_DEFAULTS,
                Include.USE_DEFAULTS, null, null);

public enum Include
{
        /**
         * Value that indicates that property is to be always included,
         * independent of value of the property.
         */
        ALWAYS,
    	..............
}

就能知道使用方法改为了:

@JsonInclude(JsonInclude.Include.NON_NULL)

这样虽然可能对像我这样的初级程序员来说,花费的时间比百度更慢,但是我认为,阅读他人的源码,可以类比发现我们自己代码中的不足,提升自我的编码水平,还是相当值得的,不然一辈子都只能面向百度编程,我的目标可是架构师,加油!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值