Swagger 从RESTAPI的json文档响应招摇过市中删除对象的某些属性
嗨,我正在使用swagger来记录我的RESTfulWeb服务。想知道有没有办法从json文档响应中删除对象的某些属性?我的意思是,swagger为我的方法param对象和响应模型(例如notes、defaultValue、allowableValue、internalDescription等)提供了很多属性,这些属性对我来说不是必需的,并且是空的,因为响应的可读性不强 对于方法参数:Swagger 从RESTAPI的json文档响应招摇过市中删除对象的某些属性,swagger,Swagger,嗨,我正在使用swagger来记录我的RESTfulWeb服务。想知道有没有办法从json文档响应中删除对象的某些属性?我的意思是,swagger为我的方法param对象和响应模型(例如notes、defaultValue、allowableValue、internalDescription等)提供了很多属性,这些属性对我来说不是必需的,并且是空的,因为响应的可读性不强 对于方法参数: "parameters": [ {
"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"
}
}
您的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中的扫描属性中,空值将消失。您的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中的扫描属性中,空值将消失