Springboot @RequestBody + Retrofit @Body 之实体类字段含有日期Date类型的提交方式

12 篇文章 0 订阅
2 篇文章 0 订阅

有时候进行参数提交时需要提交很多的参数,一个个参数写太麻烦,则可以利用POST即两者的注解直接实体类进行提交,他们会自动进行字段的映射,相当于json数据格式提交,可以省很多事,但是需要注意字段为日期Date类型时的处理。

演示代码:

springboot controller类:

@RequestMapping("addNewConference/")
    public BaseResult<Conference> addNewConference(@RequestBody Conference conference) {
        System.out.println(conference);
        // 如果id不存在,则添加
        if (conference.getConferenceId() == null) {
            conference.setReleaseTime(new Date());
            conference.setStatus(Conference.STATUS_SOON);
            if (conferenceService.insert(conference)) {
                ConferencePushUtils.PushConference(conference);//推送会议
                return ResultUtils.getSuccBaseResult(conference);
            } else {
                return ResultUtils.getErrorBaseResult(3, "会议发布失败");
            }
        }
        // 否则更新
        conferenceService.updateById(conference);
        return ResultUtils.getSuccBaseResult(conference);
    }

Retrofit接口:

//发布会议
    @POST("/conference/addNewConference/")
    Call<BaseResult> sendNewMeeting(@Body Conference conference);

Retrofit 实例化:

Gson gson=new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
        Retrofit retrofit = new Retrofit.Builder().client(client).baseUrl(Api.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create(gson)).build();

Conference 实体类 —–关键在于给date日期加上格式化format

package com.glut.meetingsign.model;

import com.baomidou.mybatisplus.enums.IdType;
import com.fasterxml.jackson.annotation.JsonFormat;

import java.util.Date;
import com.baomidou.mybatisplus.annotations.TableId;
import com.baomidou.mybatisplus.activerecord.Model;
import com.baomidou.mybatisplus.annotations.TableName;
import java.io.Serializable;

/**
 * <p>
 * 
 * </p>
 *
 * @author FlyMegoc
 * @since 2017-05-03
 */
@TableName("me_conference")
public class Conference extends Model<Conference> {

    public final static int STATUS_SOON=1;//即将开始
    public final static int STATUS_CANCLE=2;//已取消
    public final static int STATUS_END=3;//已结束

    private static final long serialVersionUID = 1L;

    /**
     * 会议id
     */
    @TableId(value="conferenceId", type= IdType.AUTO)
    private Integer conferenceId;
    /**
     * 会议主题
     */
    private String title;
    /**
     * 会议内容
     */
    private String content;
    /**
     * 会议时间
     */
    @JsonFormat(locale="zh", timezone="GMT+8", pattern="yyyy-MM-dd HH:mm:ss")
    private Date conferenceTime;
    /**
     * 会议状态:即将开始、已经取消、已经结束
     */
    private Integer status;
    /**
     * 会议地点
     */
    private String address;
    /**
     * 发布时间
     */
    @JsonFormat(locale="zh", timezone="GMT+8", pattern="yyyy-MM-dd HH:mm:ss")
    private Date releaseTime;
    /**
     * 发布人id
     */
    private Integer userId;


    public Integer getConferenceId() {
        return conferenceId;
    }

    public void setConferenceId(Integer conferenceId) {
        this.conferenceId = conferenceId;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public Date getConferenceTime() {
        return conferenceTime;
    }

    public void setConferenceTime(Date conferenceTime) {
        this.conferenceTime = conferenceTime;
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public Date getReleaseTime() {
        return releaseTime;
    }

    public void setReleaseTime(Date releaseTime) {
        this.releaseTime = releaseTime;
    }

    public Integer getUserId() {
        return userId;
    }

    public void setUserId(Integer userId) {
        this.userId = userId;
    }

    public static final String CONFERENCEID = "conferenceId";

    public static final String TITLE = "title";

    public static final String CONTENT = "content";

    public static final String CONFERENCETIME = "conferenceTime";

    public static final String STATUS = "status";

    public static final String ADDRESS = "address";

    public static final String RELEASETIME = "releaseTime";

    public static final String USERID = "userId";

    @Override
    protected Serializable pkVal() {
        return this.conferenceId;
    }

    @Override
    public String toString() {
        return "Conference [conferenceId=" + conferenceId + ", title=" + title + ", content=" + content
                + ", conferenceTime=" + conferenceTime + ", status=" + status + ", address=" + address
                + ", releaseTime=" + releaseTime + ", userId=" + userId + "]";
    }

}

关键点:对实体类的Date日期类型字段进行格式化操作,且保证两者的格式化格式一致

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值