Skip to content

How to Configure allowableValues in Swagger Annotations for Spring Boot REST APIs

Purpose

This post demonstrates how to configure allowed parameter values using Swagger annotations in Spring Boot REST APIs, so that swagger.json contains proper enum specifications.

Environment

  • Spring Boot with JAX-RS
  • Maven project
  • swagger-core 1.6.2
  • swagger-annotations 1.6.2

The Annotations

Swagger provides two annotations for specifying allowed parameter values:

  • @ApiImplicitParam - for parameters not directly in method signature
  • @ApiParam - for method parameters

Both support the allowableValues property.

Using @ApiImplicitParam

When your parameter is implicit (like @QueryParam handled by JAX-RS), use @ApiImplicitParam:

LocationResource.java
@GET
@Path("/locations")
@ApiOperation("Get locations by type")
@ApiImplicitParams({
@ApiImplicitParam(
name = "poi_types",
value = "Types of locations to include",
allowableValues = "pos, wifi, country",
dataType = "string",
paramType = "query"
)
})
public Response getLocations(@QueryParam("poi_types") String poiTypes) {
// implementation
}

Key properties:

  • name - parameter name matching your @QueryParam
  • value - description for documentation
  • allowableValues - comma-separated list of allowed values
  • dataType - typically “string”
  • paramType - “query” for query parameters

Using @ApiParam

When your parameter is directly in the method signature, use @ApiParam:

CountryResource.java
@GET
@Path("/countries")
public Response getCountries(
@QueryParam("isocc2")
@ApiParam(
name = "isocc2",
value = "Country code",
allowableValues = "en, es, us, cn"
)
String countryCode) {
// implementation
}

Maven Dependencies

Add these to your pom.xml:

pom.xml
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-core</artifactId>
<version>1.6.2</version>
</dependency>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-annotations</artifactId>
<version>1.6.2</version>
</dependency>

Verifying the Output

After configuring annotations, check the generated swagger.json:

swagger.json parameter excerpt
{
"name": "poi_types",
"in": "query",
"description": "Types of locations to include",
"required": false,
"type": "string",
"enum": ["pos", "wifi", "country"]
}

The enum array should contain your specified values.

How to Verify

  1. Start your application
  2. Access swagger.json endpoint (usually /swagger.json or /v2/api-docs)
  3. Search for your parameter in the JSON
  4. Confirm the enum array matches your allowableValues

If the enum array is correct, your annotation configuration is working.

Common Issues

  1. Values not appearing in swagger.json - Check annotation syntax and ensure swagger-core is configured to scan your resources.

  2. Swagger UI not showing dropdown - This is a UI rendering issue, not configuration. See related troubleshooting posts.

  3. Multiple parameters - Use @ApiImplicitParams wrapper for multiple @ApiImplicitParam annotations.

Summary

In this post, I showed how to configure allowableValues in Swagger annotations. The key point is using comma-separated values in @ApiImplicitParam or @ApiParam annotations. After configuration, verify swagger.json contains the expected enum array.

Final Words + More Resources

My intention with this article was to help others share my knowledge and experience. If you want to contact me, you can contact by email: Email me

Here are also the most important links from this article along with some further resources that will help you in this scope:

Oh, and if you found these resources useful, don’t forget to support me by starring the repo on GitHub!

Comments