问题
I am creating my spring restful service documentation with swagger.
Sample request handler method and its documantation below. It works:
@ApiOperation(value = "Returns user details", notes = "Returns a complete list of users details with a date of last modification.", response = User.class)
public Response getUser(@ApiParam(name = "userName", value = "Alphanumeric login to the application", required = true) @PathParam("userName") String userName) {
...
}
But I want to provide text of the request handler methods into swagger annotations from a properties file (It is being done to not increase the class size with documentation text).
Therefore I have created a properties file called "application-swagger.properties" and enabled it in "application.properties" (spring.profiles.active=swagger).
When I create api documentation as below, Text is not being displayed.
application-swagger.properties:
value=Returns user details
notes=Returns a complete list of users details with a date of last modification.
Controller class:
@Controller
@Profile("swagger")
public class UserController{
.
.
@ApiOperation(value = "${value}", notes = ${notes}", response = User.class)
public Response getUser(@ApiParam(name = "userName", value = "Alphanumeric login to the application", required = true) @PathParam("userName") String userName) {
...
}
.
.
}
Is there a way of putting text to swagger annotation from a properties file? Thanks for your help.
回答1:
It has been quiet late to answer the question but in case anyone needs help regarding the same may refer it.
Steps:
- Create a property file, e.g. swagger.properties
- Enter your desired messages as key-value pairs where key will be used as placeholder - e.g. person.id=Unique identifier of the person
- Instead of annotation text insert a placeholder - e.g. ${person.id}
- Register the property file in your configuration on class level - eg. @PropertySource("classpath:swagger.properties")
Worked with Spring Boot 1.5 and Swagger 2.8, not sure about the previous versions.
Reference taken from a detailed article about the same.
回答2:
You cannot do this, as the annotation value expects a constant, but in your scenario it is not a constant. Please refer the accected values in the annotaion java doc, says it must be a constant.
The easiest/readable way to do this in your case is to move all the constants in a separate class instead of property file.
来源:https://stackoverflow.com/questions/41737779/how-to-provide-texts-to-swagger-in-a-properties-file-for-spring-restful-service