Swagger 2 API documentation integration library for Spring WebMVC and WebFlux applications
—
Customized Jackson module for proper Swagger 2 JSON serialization with support for vendor extensions, example values, and Swagger-specific formatting requirements.
Custom Jackson module that configures JSON serialization for Swagger 2 objects, ensuring proper formatting and compliance with Swagger 2 specification requirements.
/**
* Jackson module for customizing Swagger 2 JSON serialization
* Extends SimpleModule and implements JacksonModuleRegistrar for Spring integration
*/
public class Swagger2JacksonModule extends SimpleModule implements JacksonModuleRegistrar {
/**
* Conditionally registers this module with the provided ObjectMapper
* Only registers if no existing Swagger mix-in is already configured
* Also disables FAIL_ON_EMPTY_BEANS serialization failure
*
* @param objectMapper The Jackson ObjectMapper to register with
*/
public void maybeRegisterModule(ObjectMapper objectMapper);
/**
* Sets up Jackson serialization mix-ins for Swagger 2 types
* Configures custom serialization behavior for all Swagger model objects
*
* @param context Jackson module setup context
*/
public void setupModule(SetupContext context);
}Usage Example:
// Typically configured automatically by Spring Boot
ObjectMapper objectMapper = new ObjectMapper();
Swagger2JacksonModule module = new Swagger2JacksonModule();
module.maybeRegisterModule(objectMapper);
// Manual registration
objectMapper.registerModule(new Swagger2JacksonModule());The module configures custom serialization behavior for all major Swagger 2 types:
/**
* Mix-in class that configures common serialization settings for Swagger types
* Applied to: Swagger, Info, License, Scheme, SecurityRequirement, SecuritySchemeDefinition,
* Model, Operation, Path, Parameter, ExternalDocs, Xml, Tag, Contact
*/
@JsonAutoDetect
@JsonInclude(value = JsonInclude.Include.NON_EMPTY)
private class CustomizedSwaggerSerializer {
}
/**
* Mix-in class specifically for Response objects
* Ignores the 'responseSchema' property during serialization
*/
@JsonAutoDetect
@JsonInclude(value = JsonInclude.Include.NON_EMPTY)
@JsonIgnoreProperties("responseSchema")
private class ResponseSerializer {
}Custom serializer for handling example values in API documentation with proper JSON formatting.
/**
* Mix-in interface for Property example serialization
* Applies custom serialization logic to example values
*/
@JsonAutoDetect
@JsonInclude(value = JsonInclude.Include.NON_EMPTY)
private interface PropertyExampleSerializerMixin {
/**
* Custom serialization for example values
* Handles string literals, JSON objects, arrays, and primitive values
*/
@JsonSerialize(using = PropertyExampleSerializer.class)
Object getExample();
}
/**
* Custom Jackson serializer for property example values
* Handles various data types and JSON formatting requirements
*/
public static class PropertyExampleSerializer extends StdSerializer<Object> {
/**
* Serializes example values with proper JSON formatting
* Handles string literals, JSON objects, arrays, and primitive types
*
* @param value The example value to serialize
* @param gen Jackson JSON generator
* @param provider Serializer provider
*/
public void serialize(Object value, JsonGenerator gen, SerializerProvider provider) throws IOException;
/**
* Determines if a value should be considered empty
* @param value The value to check
* @return true if the value is null or empty string
*/
public boolean isEmpty(Object value);
}All Swagger objects are configured with JsonInclude.Include.NON_EMPTY, which means:
The module uses @JsonAutoDetect to automatically detect:
The custom example serializer handles various data types:
// String literals (quoted strings)
"example": "hello world"
// JSON objects
"example": {"key": "value", "number": 42}
// JSON arrays
"example": [1, 2, 3, "item"]
// Boolean values
"example": true
// Numeric values
"example": 42.5
// Raw JSON (when input is already valid JSON)
"example": {"raw": "json"}The example serializer intelligently handles string literals:
/**
* Checks if a string value is a quoted literal
* @param value String to check
* @return true if starts and ends with quotes (single or double)
*/
boolean isStringLiteral(String value);
/**
* Checks if a string represents valid JSON (not a plain string)
* Detects objects, arrays, booleans, and numbers
* @param value String to check
* @return true if the string is valid JSON
*/
boolean isNotJsonString(String value);The module is automatically registered when using the provided Spring configuration:
@Configuration
@EnableSwagger2WebMvc
public class SwaggerConfig {
// Swagger2JacksonModule is automatically configured
}For custom ObjectMapper configuration:
@Configuration
public class JacksonConfig {
@Bean
@Primary
public ObjectMapper objectMapper() {
ObjectMapper mapper = new ObjectMapper();
// Register Swagger 2 module
Swagger2JacksonModule swagger2Module = new Swagger2JacksonModule();
swagger2Module.maybeRegisterModule(mapper);
return mapper;
}
}The module automatically configures these Jackson settings:
SerializationFeature.FAIL_ON_EMPTY_BEANS = false - Prevents errors on empty objectsThe module provides custom serialization for these Swagger 2 types:
Swagger - Root Swagger specification objectInfo - API metadata informationLicense - License informationContact - Contact informationOperation - API operation definitionsPath - API path definitionsParameter - Operation parametersResponse - Operation responsesModel - Data model definitionsProperty - Model property definitionsXml - XML-specific metadataSecurityRequirement - Security requirementsSecuritySchemeDefinition - Security scheme definitionsTag - Operation tags for groupingExternalDocs - External documentation linksScheme - Supported protocols (HTTP, HTTPS)The serialization produces optimized JSON output:
The Jackson module integrates with:
Install with Tessl CLI
npx tessl i tessl/maven-io-springfox--springfox-swagger2