2013-05-16 34 views
1

您好我正在使用swagger來記錄我的RESTful web服務。想知道是否有任何方法從json文檔響應中刪除對象的某些屬性?我的意思是,有很多屬性讓我的方法參數對象和響應模型(例如notes,defaultValue,allowableValue,internalDescription等)不是我所需要的,並且由於響應不太易讀而爲空從json文檔響應中刪除對象的某些屬性Swagger for REST api

對於方法PARAMS:

 "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

您的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和空值將會消失。

相關問題