我试图让Swagger从属性文件swagger.properties
中读取API文档,但不能。在@ApiOperation
注释中有一个错误提示:Attribute value must be constant
。关于如何解决此问题以及能够从属性文件中阅读文档的任何建议?这是控制器代码:
package com.demo.student.demo.controller;
import com.demo.student.demo.entity.Student;
import com.demo.student.demo.service.StudentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping(value = "/v1/students")
@Api(description = "Set of endpoints for Creating, Retrieving, Updating and Deleting of Students.")
public class StudentController {
private final String message;
public StudentController(@Value("${test.swagger.message}") String message){
this.message=message;
}
@Autowired
private StudentService studentService;
@GetMapping
@ApiOperation(message)
public List<Student> findAll(){
return studentService.findAl();
}
}
而且,如何在类级别的@API(description)中插入值?
错误消息说属性值必须是常量,Spring不能将值注入静态final字段。同样,也无法在类级别之外插入值(即@Api
注释的描述)
一种解决方法将是创建一个仅包含全部为final static Strings
的常量的类,如下所示:>
public final class Constants { public static final String API_DESCRIPTION = "description"; }
并在控制器中使用它
@Api(description = Constants.API_DESCRIPTION)
public class StudentController {
}