发送对象时忽略JSON字段(反序列化) [英] Ignoring JSON fields when sending an object (deserialization)

查看:527
本文介绍了发送对象时忽略JSON字段(反序列化)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个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 -发布评分

https://zapodaj.net/2f60536ba4326.png.html - GET the rating https://zapodaj.net/71e52684343df.png.html - POST the rating

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

When adding the rating I do not want to add a user or date, because it is automatically completed on the sevice layer. How to ignore these fields so that they are not shown at all and that the user can not complete these fields when sending the assessment POST, and that they can only be seen when displaying GET.

推荐答案

您可以将应在响应中显示但在请求中不显示的属性的readOnly属性设置为true.

You can set the readOnly attribute to true for the properties which should be shown in the response, but not in the request.

private Integer rate;

@ApiModelProperty(readOnly = true)
private String user;

@ApiModelProperty(readOnly = true)
private Date date;

swagger-fox生成的模型将是

The model generated by swagger-fox will be

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

在swagger编辑器中,它将以以下方式显示.

In the swagger editor, it will be displayed in the following manner.

这篇关于发送对象时忽略JSON字段(反序列化)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆