Display default value of a model attribute in Swagger UI - spring-boot

I am using Spring Boot Java and Swagger 2 to document my APIs.
What Spring annotation can I use to show the default value mydoggie in Swagger UI?

You can add an example and a default value to show in Swagger 2 with this annotation:
#ApiModelProperty(value = "value to show", example = "example to show")
I hope it helps

Related

Alternative way for setting default for #Value-annotated field in Spring Boot

I have a Spring Boot application written in Kotlin and I would prefer to set the default of a #Value-annotated field like below:
#Value("\${service-clients.logging.exceptions:/}")
private var loggingExceptions: Array<String> = emptyArray()
Is there anything wrong with doing it this way? Most of the resources I found on the web recommend to set the default in the annotation itself. Is there any advantage of doing it that way?

Swagger on Spring Boot Actuator

I'm documenting my API with Swagger and I'm applying the following annotation to all of my REST Controllers:
#Api(value = "Some value", description = "Some Description", tags = {"Tag Tag Tag"})
However, I also want to include the endpoints provided by Spring Boot Actuator (/health, /info) on Swagger (and they are being included), however I can't seem to find a way to change their default description, tags, and title, among other swagger properties.
Is there a way to do this? Thanks in advance.

Use of "reference" element in #ApiResponse annotation in swagger to refer response samples

I need to show the response example value with respect to each code in Swagger UI, I'm using Spring Boot + springfox-swagger2
It seems that the "reference" attribute is not been read by during the generation of the swagger file (see https://github.com/springfox/springfox/issues/1705)

generate sample example in swagger UI (in Spring boot project)

I am using a spring boot application and have configured using Swagger UI.
I want to know whether we could pre-populate the example value with sample value so we can hit the "Try it out!" button without having to type in some sample values to get a response.
It must be present there.
Is there a way we can do this using annotations or a separate file which Swagger uses?
I am using a spring boot project with springfox-swagger2:2.7.0 and springfox-swagger-ui:2.7.0 with dependencies added using gradle.
Since the #ApiParam properties example and examples are not working (see this issue on GitHub), support for adding examples is limited/non existing.
What you can do for simple parameters (#RequestParam) is to add the #ApiParam annotation with the defaultValue property, like this:
#GetMapping
public List<Foo> findAll(
#RequestParam(required = false)
#ApiParam(defaultValue = "foo") // Put the default value here
String input) {
// ...
}
However, there is no support yet for doing this with #RequestBody parameters.
A possible workaround for #RequestBody parameters is by clicking on the code box at the right side of the Swagger tester, where it says Example value. If you click on it, it will insert that example into the field itself.
Here is a workaround for providing an example:
Inject html into swagger
#ApiParam(
name="whatever",
value="whatever",
defaultValue="none</p><p>Example: xyz</p>"
)
They don't protect against it in the latest version 2.9.2.

JSR-303/JSR-349 message key in Spring MVC 4

I have an application (Spring MVC 4 + Hibernate/JPA + MySQL + Maven integration example using annotations), integrating Spring with Hibernate using annotation based configuration.
I have this property
#Pattern(regexp = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*#"
+ "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$",
message = "{general.error.mail}")
private String email;
But when there is an error I see the key in the page instead of the message itself
{general.error.mail}
You need a messages.properties file. Check Spring MVC Form Validation Example with Bean Validation API as a tutorial.

Resources