You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Swagger UI未展示自定义Employee模型全部字段的问题求助

问题描述

我正在学习Swagger,搭建了一个简单的Spring Boot REST API用于展示员工信息。Employee模型包含员工地址关联,尝试通过Swagger展示员工详情文档时遇到问题:访问Swagger UI时仅能看到listOfAddresses字段,员工的id和name字段缺失。

Employee模型代码

package com.example.documentation.model;

import io.swagger.v3.oas.annotations.media.Schema;

import java.util.Arrays;
import java.util.List;

public class Employee<T> {
    @Schema(type = "int", description = "id for employee")
    private int id;

    @Schema(type = "string", description = "name of employee")
    private String name;
    private List<T> listOfAddresses;
    
    public Employee(int id, String name, List<T> listOfAddresses) {
        this.listOfAddresses = listOfAddresses;
        this.id = id;
        this.name = name;
    }

    public List<T> getListOfAddresses() {
        listOfAddresses = (List<T>) Arrays.asList(new Address(2201,"Street 1","UB12 1TT"), new Address(3201,"Street 1","KK12 1TP"));
        return listOfAddresses;
    }

    public void setListOfAddresses(List<T> listOfAddresses) {
        this.listOfAddresses = listOfAddresses;
    }
}

其中Address是包含houseNumber、Streetname和postcode的普通POJO。

控制器代码

package com.example.documentation.controller;

import com.example.documentation.model.Address;
import com.example.documentation.model.Employee;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

@RestController
public class EmployeeController {

    private Employee<Address> employee;

    @RequestMapping("/employee/{id}")
    @Operation(
            tags = {"Employee"}
    )

    public Employee<Address> getEmployeeDetailsById(@PathVariable(name = "id", required = true) int id,
                                                    @PathVariable(name = "name", required = false) String name)  {

        List<Address> list = Arrays.asList(new Address(101,"T1","Test 1"),
                new Address(201,"T2","Test 2"));
        employee = new Employee<>(101,"David", list);

        return employee;
    }
}

项目使用Gradle构建。

解决方案

问题核心是Employee类的id和name字段缺少对应的getter方法,Swagger依赖JavaBean规范扫描类属性,只有提供getter方法的字段才会被识别并展示在文档中。

修正步骤:

  • 给id和name字段添加标准的getter(及可选的setter)方法:
public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}
  • 同时修复getListOfAddresses()方法的问题:当前每次调用都会覆盖构造函数传入的地址列表,改为返回实例自身的listOfAddresses字段:
public List<T> getListOfAddresses() {
    return listOfAddresses;
}

重新构建启动项目后,Swagger UI就能正常显示id、name和listOfAddresses三个字段了。

内容的提问来源于stack exchange,提问作者Indu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 07:21:36