发送对象时忽略JSON字段(反序列化)

时间:2018-05-25 15:16:41

标签: java json spring

我有一个DTO对象

public class Rate {
    private final Integer rate;
    private final String user;
    private final Date date;
}

和两个控制器

https://zapodaj.net/2f60536ba4326.png.html - 获得评分 https://zapodaj.net/71e52684343df.png.html - 发布评分

添加评级时,我不想添加用户或日期,因为它会在服务层自动完成。如何忽略这些字段,以便它们完全不显示,并且用户在发送评估POST时无法完成这些字段,并且只有在显示GET时才能看到它们。

1 个答案:

答案 0 :(得分:2)

对于应在响应中显示但不在请求中的属性,可以将readOnly属性设置为true。

private Integer rate;

@ApiModelProperty(readOnly = true)
private String user;

@ApiModelProperty(readOnly = true)
private Date date;

swagger-fox生成的模型将是

"definitions": {
    "Obj": {
        "type": "object",
        "properties": {
            "date": {
                "type": "string",
                "format": "date-time",
                "readOnly": true
            },
            "rate": {
                "type": "integer",
                "format": "int32"
            },
            "user": {
                "type": "string",
                "readOnly": true
            }
        }
    }
}

在招摇编辑器中,它将以下列方式显示。

enter image description here

相关问题