首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >从json document response Swagger for REST api中删除对象的某些属性

从json document response Swagger for REST api中删除对象的某些属性
EN

Stack Overflow用户
提问于 2013-05-16 15:16:56
回答 1查看 1.8K关注 0票数 1

您好,我正在使用swagger为我的RESTful web服务编写文档。想知道有什么方法可以从json文档响应中删除对象的某些属性吗?我的意思是,swagger为我的方法参数对象和响应模型提供了很多属性(例如notes、defaultValue、allowableValue、internalDescription等)。对于我来说不是必需的,并且由于响应的可读性不是很高而为空

对于方法参数:

代码语言:javascript
运行
复制
     "parameters": [
                    {
                        "name": "someName1",
                        "description": null,
                        "notes": null,
                        "paramType": "path",
                        "defaultValue": null,
                        "allowableValues": null,
                        "required": true,
                        "allowMultiple": false,
                        "paramAccess": null,
                        "internalDescription": null,
                        "wrapperName": null,
                        "dataType": "string",
                        "valueTypeInternal": null
                    },
                    {
                        "name": "someName2",
                        "description": null,
                        "notes": null,
                        "paramType": "query",
                        "defaultValue": null,
                        "allowableValues": null,
                        "required": true,
                        "allowMultiple": false,
                        "paramAccess": null,
                        "internalDescription": null,
                        "wrapperName": null,
                        "dataType": "string",
                        "valueTypeInternal": null
                    }
                ],

-=============================================================================

对于响应模型类

代码语言:javascript
运行
复制
"SomeResponseClass": {
        "required": false,
        "name": null,
        "id": "SomeResponseClass",
        "properties": {
            "instanceVariable1": {
                "required": false,
                "name": null,
                "id": null,
                "properties": null,
                "allowableValues": null,
                "description": null,
                "notes": null,
                "access": null,
                "default": null,
                "additionalProperties": null,
                "items": null,
                "uniqueItems": false,
                "type": "Date"
            },
            "instanceVariable2": {
                "required": false,
                "name": null,
                "id": null,
                "properties": null,
                "allowableValues": null,
                "description": null,
                "notes": null,
                "access": null,
                "default": null,
                "additionalProperties": null,
                "items": null,
                "uniqueItems": false,
                "type": "double"
            }
         }
EN

回答 1

Stack Overflow用户

发布于 2013-05-16 22:40:14

您的JSON映射器未配置为忽略空属性。您可以轻松地解决这个问题,如下所示:

代码语言:javascript
运行
复制
@Provider
@Produces(MediaType.APPLICATION_JSON)
public class JacksonJsonProvider extends JacksonJaxbJsonProvider {
private static ObjectMapper commonMapper = null;

public JacksonJsonProvider() {
    if(commonMapper == null){
        ObjectMapper mapper = new ObjectMapper();

        mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        mapper.setSerializationInclusion(JsonInclude.Include.NON_DEFAULT);
        mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

        commonMapper = mapper;
    }
    super.setMapper(commonMapper);
    }
}

将此映射器添加到web.xml中的扫描属性中,空值就会消失。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16581249

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档