如何将自定义Jackson ObjectMapper注册到Swagger以配置字段展示?
我来帮你搞定把Jackson Mixin集成到Swagger文档生成的问题,步骤很清晰,咱们一步步来:
首先得把Mixin类写好,把你想要的字段展示规则(比如驼峰命名)定义进去。举个例子,假设你的请求类SomeRequest里有下划线命名的字段,想要在Swagger里显示成驼峰:
import com.fasterxml.jackson.annotation.JsonProperty; // Mixin类,只定义字段的序列化规则,不用实现任何方法 public abstract class SomeRequestMixin { @JsonProperty("firstName") private String first_name; // 把原字段first_name映射成Swagger里的firstName }
然后把这个Mixin注册到自定义的ObjectMapper里,用Spring Bean的方式:
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.PropertyNamingStrategies; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class JacksonConfig { @Bean public ObjectMapper customObjectMapper() { ObjectMapper mapper = new ObjectMapper(); // 注册Mixin到对应的实体类 mapper.addMixIn(SomeRequest.class, SomeRequestMixin.class); // 如果需要全局的驼峰命名策略,也可以直接设置(和Mixin二选一或配合使用) // mapper.setPropertyNamingStrategy(PropertyNamingStrategies.LOWER_CAMEL_CASE); return mapper; } }
因为你用的是io.swagger.annotations(也就是Springfox Swagger 2),需要在Swagger的配置类里,让它用你的自定义ObjectMapper来解析模型字段。
先创建Swagger的配置类,注入你刚才定义的customObjectMapper,然后修改Docket的配置:
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; import com.fasterxml.jackson.databind.ObjectMapper; @Configuration @EnableSwagger2 public class SwaggerConfig { private final ObjectMapper customObjectMapper; // 构造方法注入自定义的ObjectMapper public SwaggerConfig(ObjectMapper customObjectMapper) { this.customObjectMapper = customObjectMapper; } @Bean public Docket api() { return new Docket(DocumentationType.SWAGGER_2) .select() // 替换成你的控制器所在包路径 .apis(RequestHandlerSelectors.basePackage("com.yourcompany.yourproject.api")) .paths(PathSelectors.any()) .build() // 关键操作:让Swagger使用自定义ObjectMapper处理模型 .additionalModels(typeResolver -> { typeResolver.setObjectMapper(customObjectMapper); return null; }) .useDefaultResponseMessages(false); } }
你说的转换器工厂类,应该是Swagger的ModelConverter或ModelConverterFactory实现类。这类类只要加上@Component注解,让Spring能扫描到它就行,不需要放到特定目录——只要它在Spring的组件扫描范围内(比如和你的启动类同包或子包),Spring会自动把它注册到Swagger的模型转换链里。
举个简单的实现例子:
import org.springframework.stereotype.Component; import springfox.documentation.schema.ModelConverter; import springfox.documentation.schema.ModelConverterContext; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spi.schema.ModelConverterPlugin; import com.fasterxml.jackson.databind.ObjectMapper; @Component public class CustomModelConverter implements ModelConverterPlugin { private final ObjectMapper objectMapper; public CustomModelConverter(ObjectMapper objectMapper) { this.objectMapper = objectMapper; } @Override public void addModelConverter(ModelConverter converter) { // 这里可以利用objectMapper的规则,自定义模型字段的转换逻辑 // 比如处理Mixin里的注解,确保Swagger能识别 } @Override public boolean supports(DocumentationType documentationType) { // 支持所有Swagger文档类型 return true; } }
启动你的Spring Boot应用,访问默认的Swagger UI地址http://localhost:8080/swagger-ui.html,找到你的/api/blah/some_endpoint接口,查看请求体SomeRequest和响应体SomeResponseClass的字段,应该已经按照你Mixin里的配置显示成驼峰命名了。
内容的提问来源于stack exchange,提问作者user3301081

