从json文档响应Swagger for REST api中删除对象的某些属性

时间:2013-05-16 07:16:56

标签: swagger

您好我使用swagger来记录我的RESTful Web服务。想知道有没有办法从json文档响应中删除对象的某些属性?我的意思是有很多属性为我的方法param对象和响应模型(例如notes,defaultValue,allowableValue,internalDescription等)提供了swagger,这些属性对我来说并不是必需的,并且由于响应不太可读而为null

方法参数:

     "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
                    }
                ],

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

对于响应模型类

"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"
            }
         }

1 个答案:

答案 0 :(得分:1)

您的JSON映射器未配置为忽略空属性。您可以按如下方式轻松解决此问题:

@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中的扫描属性中,空值将消失。