1

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

对于方法参数:

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

1 回答 1

1

your JSON mapper is not configured to ignore null properties. You can easily address this as follows:

@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);
    }
}

Add this mapper to your scanning properties in the web.xml and the nulls will be gone.

于 2013-05-16T14:40:13.793 回答