Swagger 属性名 FastJson支持

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

在Swagger 里面默认用的是Jackson 序列化

对于我们属性全是用的FastJson的来说的话 很痛苦

Swagger 属性名 FastJson支持_第1张图片

参数名全成了默认属性名

Swagger提供的默认注解 ApiModelProperty 的name属性也是完全不起作用的

113518_a9KQ_2760304.png

如果项目没有配置Jackson的话 swagger是运行不起来的

贴配置:

@Configuration
@EnableSwagger2
public class Swagger2 {

    @Autowired
    private ApplicationContext applicationContext;


    @PostConstruct
    public void setObjectMapper() {
        ObjectMapper objectMapper = new ObjectMapper();
        SimpleModule module = new SimpleModule();
        objectMapper.registerModule(module);

        objectMapper.setAnnotationIntrospector(new JacksonAnnotationIntrospector() {

            @Override
            public boolean isAnnotationBundle(Annotation ann) {
                if (ann.annotationType() == JSONField.class) {
                    return true;
                }
                return super.isAnnotationBundle(ann);
            }

            @Override
            public PropertyName findNameForSerialization(Annotated a) {
                PropertyName nameForSerialization = super.findNameForSerialization(a);
                if (nameForSerialization == null || nameForSerialization == PropertyName.USE_DEFAULT) {
                    JSONField jsonField = _findAnnotation(a, JSONField.class);
                    if (jsonField != null) {
                        return PropertyName.construct(jsonField.name());
                    }
                }
                return nameForSerialization;
            }

            @Override
            public PropertyName findNameForDeserialization(Annotated a) {
                PropertyName nameForDeserialization = super.findNameForDeserialization(a);
                if (nameForDeserialization == null || nameForDeserialization == PropertyName.USE_DEFAULT) {
                    JSONField jsonField = _findAnnotation(a, JSONField.class);
                    if (jsonField != null) {
                        return PropertyName.construct(jsonField.name());
                    }
                }
                return nameForDeserialization;
            }
        });

        ObjectMapperConfigured objectMapperConfigured = new ObjectMapperConfigured(applicationContext, objectMapper);
        applicationContext.publishEvent(objectMapperConfigured);
    }

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com."))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("在线接口文档")
                .contact("[email protected]")
                .version("3.2")
                .build();
    }

}

Jackson配置包含

JacksonAnnotationIntrospector

主要是用来对注解JSONFiled的支持

114115_pCja_2760304.png

Swagger 属性名 FastJson支持_第2张图片

加上后的效果

转载于:https://my.oschina.net/wallxue/blog/1559736

你可能感兴趣的:(python)