SpringBoot中Swagger无法显示Model,DTO在Swagger UI为空求助
Hey there! Let's walk through the common reasons why your PatientDto is showing up empty in Swagger UI and how to fix them. I've dealt with this exact issue a few times, so here are the key checks you should run:
PatientDto Annotations & Structure Swagger relies on proper annotations and JavaBean conventions to pick up model fields. Let's start here:
- Add Swagger-compatible annotations: Depending on whether you're using Springfox (Swagger 2) or SpringDoc (OpenAPI 3), use the right annotations to mark your DTO and fields:
For Springfox (Swagger 2):
For SpringDoc (OpenAPI 3):import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; @ApiModel(description = "DTO representing patient details") public class PatientDto { @ApiModelProperty(value = "Unique patient ID", example = "456") private Long id; @ApiModelProperty(value = "Patient's full name", example = "Jane Smith") private String fullName; // Critical: Don't skip getters and setters! public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getFullName() { return fullName; } public void setFullName(String fullName) { this.fullName = fullName; } }import io.swagger.v3.oas.annotations.media.Schema; @Schema(description = "DTO representing patient details") public class PatientDto { @Schema(description = "Unique patient ID", example = "456") private Long id; @Schema(description = "Patient's full name", example = "Jane Smith") private String fullName; // Getters and setters are still required! } - Ensure getters exist: Swagger follows JavaBean rules, so private fields need corresponding getters to be detected. No getters = empty model in UI.
- Avoid accidental exclusion: Check if any fields or getters have
@JsonIgnore(Jackson) or@Hidden(Swagger) annotations—these will make Swagger ignore the field entirely.
SwaggerConfig Setup Your configuration class might be missing key settings to scan or include your DTOs:
- Package scanning: Make sure your
Docket(Springfox) orOpenAPI(SpringDoc) bean is scanning the package where your DTOs (and controllers) live. For example, with Springfox:@Configuration @EnableSwagger2 public class SwaggerConfig { @Bean public Docket api() { return new Docket(DocumentationType.SWAGGER_2) .select() .apis(RequestHandlerSelectors.basePackage("com.yourproject")) // Scan root package including DTOs and controllers .paths(PathSelectors.any()) .build(); } } - Enable correct annotations: Double-check you're using the right enable annotation:
@EnableSwagger2for Springfox,@EnableOpenApifor SpringDoc. - Check for exclusion rules: Ensure your config isn't accidentally filtering out models or controllers that reference
PatientDto.
Version mismatches between Spring Boot and Swagger libraries are a frequent culprit:
- Spring Boot 2.6+: Older Springfox versions (like 2.9.x) break due to changes in Spring's path matching rules. Upgrade to Springfox 3.x, or switch to SpringDoc OpenAPI (which has better support for newer Spring Boot versions).
- Spring Boot 3.x: Springfox is no longer supported—you must use SpringDoc OpenAPI 2.x or later.
PatientDto Is Used in Your Controllers Swagger only displays models that are explicitly referenced by your API endpoints. If your PatientDto isn't used as a request body, response body, or parameter in any controller method, it won't show up. Example of a valid controller usage:
@RestController @RequestMapping("/patients") public class PatientController { @PostMapping @ApiOperation("Create a new patient") // Springfox; use @Operation for SpringDoc public ResponseEntity<PatientDto> createPatient(@RequestBody PatientDto patientDto) { // Your business logic here return ResponseEntity.ok(patientDto); } }
- Clean and rebuild: IDE or build cache can cause weird issues. Run
mvn clean install(Maven) or./gradlew clean build(Gradle) to refresh your build. - Check ObjectMapper config: If you've customized your Jackson
ObjectMapperto ignore properties or disable serialization, it might interfere with Swagger's model parsing. Ensure Swagger has access to a properly configured mapper.
If none of these steps resolve the issue, sharing your actual PatientDto and SwaggerConfig code will help pinpoint the exact problem!
内容的提问来源于stack exchange,提问作者Mitesh Upadhyay




