Understanding the @Hidden Annotation in Spring for Hiding OpenAPI Endpoints

Alexander Obregon
10 min readSep 3, 2023

--

Image Source

Introduction

When developing APIs in Spring Boot, it’s crucial to document them properly so that the developers consuming the APIs have a clear understanding of how they work. Spring integrates smoothly with tools like Swagger for auto-generating this documentation. However, sometimes there may be the need to hide certain API endpoints from the auto-generated OpenAPI documentation for various reasons. This is where the @Hidden annotation comes into play.

In this post, we will delve into how the @Hidden annotation can be used in Spring-based applications to hide specific API endpoints from appearing in the auto-generated OpenAPI documentation.

What is OpenAPI and Why You Need It?

OpenAPI, formerly known as Swagger, is a robust and widely adopted framework for describing RESTful APIs. It acts as a contract that defines all aspects of an API, covering the routes (endpoints), input/output parameters, HTTP methods, and even the status codes that are expected to be returned. An OpenAPI document is written in either JSON or YAML and provides a comprehensive overview of what an API can do without needing to delve into the code that powers it.

Evolution from Swagger to OpenAPI

Swagger was initially released in 2011 as a specification and a large ecosystem for building API-related tools (documentation, client SDK generation, etc.). In 2015, the Swagger Specification was donated to the OpenAPI Initiative, becoming the foundation of the OpenAPI Specification (OAS). This marked the evolution of Swagger to OpenAPI, although the term “Swagger” is still commonly used when referring to the tools and ecosystem, whereas “OpenAPI” refers to the specification itself.

Advantages of Using OpenAPI

Standardization

The first significant advantage of using OpenAPI is that it provides a standard, unified way of describing APIs. This standardization is crucial for microservices or any distributed architecture that relies on third-party APIs. Developers across different teams can understand the functionalities and constraints of an API just by looking at its OpenAPI document, fostering collaboration and reducing ambiguity.

Documentation Automation

Imagine the task of manually documenting every route, parameter, request, and response. This could be a daunting task, and any changes to the API would necessitate a complete overhaul of the documentation. OpenAPI allows for automated, dynamic documentation that changes as your API evolves. Tools like Swagger UI offer a visually rich interface where users can understand and even try out the API without any additional setup.

Client Code Generation

APIs are useless if they are not consumed. Consuming APIs usually involves writing client-side code that makes HTTP requests to the API servers. With OpenAPI, you can automatically generate client libraries, code samples, and even API documentation. This capability is supported in multiple languages and can significantly accelerate frontend and backend development, as developers won’t have to write this boilerplate code manually.

Improved Quality and Governance

Standardizing how APIs are described allows organizations to introduce quality checks and governance standards more easily. Teams can enforce policies and rules across all APIs, ensuring that only approved and well-documented services are deployed.

Facilitates Testing and Debugging

Having a detailed API contract allows for easier automated testing. Test cases can be automatically generated to ensure that the API complies with its specification. This significantly reduces the chances of human error during testing and ensures that the API remains robust and reliable.

Easy Integration with Other Tools

The OpenAPI ecosystem is rich with a variety of tools that can be used for purposes ranging from security auditing to automated code generation. The standard nature of OpenAPI means that integrating with these tools is often straightforward, further extending the advantages it offers.

What is the @Hidden Annotation?

The @Hidden annotation is an advanced feature provided by the Springfox library, a well-known toolset for integrating Swagger (OpenAPI) into Spring-based applications. While the primary purpose of Springfox is to generate OpenAPI documentation, the @Hidden annotation serves a more specialized role. It allows you to selectively exclude specific controllers or controller methods from the auto-generated OpenAPI documentation.

Origin and Significance

The concept behind hiding certain API endpoints might seem counterintuitive at first. After all, one of the primary benefits of OpenAPI documentation is to offer a complete, transparent view of all available API endpoints. However, in real-world applications, there are valid reasons to not disclose some API routes, such as internal endpoints, experimental features, or deprecated methods. This is where @Hidden comes into play, offering a layer of control over what should be publicly documented and what shouldn't.

Internal Mechanism

When you run your Spring Boot application, Springfox scans all controller classes and their corresponding methods to produce an OpenAPI specification. This process is automatic and happens at runtime. During this scan, if Springfox encounters the @Hidden annotation on a controller or method, it deliberately omits it from the final OpenAPI document. This ensures that the hidden APIs are not exposed to the consumers who rely on this document for integration.

Compatibility

Although @Hidden is widely used in Spring-based applications, it's crucial to note that this annotation is not part of the standard OpenAPI specification. Rather, it's an extension provided by Springfox. This means that if you plan to migrate to another OpenAPI toolset, you may need to find an alternative way to hide specific endpoints, as @Hidden is specific to Springfox.

Annotation Versus Configuration

It’s worth mentioning that Springfox also allows you to hide endpoints using manual configuration of the Docket bean, which is the primary configuration class for Springfox. However, using @Hidden offers a more granular level of control directly at the source code level, making it easier to manage when you're dealing with a large codebase.

How to Use the @Hidden Annotation

Utilizing the @Hidden annotation in a Spring-based application to control API visibility is a straightforward process. It offers a simple way to manage the documentation of specific endpoints without changing the core functionality of your application. However, to implement it effectively, it's essential to understand its capabilities and limitations in various scenarios.

Prerequisites

Before diving into the details, make sure you have the Springfox dependency added to your project. This is necessary because the @Hidden annotation is a part of the Springfox library.

For a Maven project, add the following dependency to your pom.xml:

<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>

Hiding an Entire Controller

To prevent all endpoints of a controller from showing up in the generated OpenAPI documentation, simply annotate the controller class with @Hidden.

@RestController
@Hidden
public class HiddenController {
@GetMapping("/hiddenEndpoint")
public String hiddenEndpoint() {
return "This endpoint will be hidden.";
}
}

When using @Hidden at the controller level, it acts as a blanket rule, concealing all of the controller's endpoints, regardless of their individual configurations.

Hiding Specific Methods

In contrast to hiding an entire controller, you can also target individual methods within a controller class. Annotate only the specific methods that you want to hide.

@RestController
public class PartiallyHiddenController {
@GetMapping("/visibleEndpoint")
public String visibleEndpoint() {
return "This endpoint is visible.";
}

@Hidden
@GetMapping("/hiddenEndpoint")
public String hiddenEndpoint() {
return "This endpoint will be hidden.";
}
}

Mixing Visible and Hidden Methods

It’s worth noting that you can mix visible and hidden methods within the same controller. This provides great flexibility in managing the API documentation at a granular level.

@RestController
public class MixedVisibilityController {
@GetMapping("/publicEndpoint")
public String publicEndpoint() {
return "This is a public endpoint.";
}

@Hidden
@GetMapping("/privateEndpoint")
public String privateEndpoint() {
return "This is a private endpoint.";
}
}

Understanding Inheritance and Overriding

If you have a controller that extends another class or implements an interface, the @Hidden annotation works in tandem with Java's inheritance rules. For example, if you hide a method in a parent class, it will be hidden in all child classes unless explicitly overridden.

@Hidden
public class ParentController {
@GetMapping("/inheritedEndpoint")
public String inheritedEndpoint() {
return "This is an inherited endpoint.";
}
}

@RestController
public class ChildController extends ParentController {
// The inherited 'inheritedEndpoint' will be hidden unless overridden here.
}

Troubleshooting and Verifying

After annotating your controllers or methods with @Hidden, it's advisable to manually check the generated OpenAPI documentation to ensure that the specified endpoints are actually hidden. Sometimes, misconfiguration or conflicting annotations could lead to unexpected behavior, so it's good to verify.

Common Use Cases for @Hidden Annotation

While it might seem counterproductive to hide API endpoints when one of the main benefits of OpenAPI is to document APIs comprehensively, there are several situations where the @Hidden annotation is not just beneficial but also necessary. Let's delve into some of the most common use-cases where you might find the @Hidden annotation useful.

Internal Endpoints

Many applications have internal endpoints that are used for administrative tasks, debugging, or are restricted to certain groups within an organization. These endpoints aren’t intended for public consumption and can expose sensitive data or functionalities. Using @Hidden, you can effectively keep these routes out of the OpenAPI documentation, reducing the risk of misuse.

@Hidden
@GetMapping("/internal/debug")
public DebugInfo getDebugInfo() {
// Return debug information
}

Experimental Features

As you develop new features or improvements, you might deploy endpoints that are in an experimental phase and are subject to change or removal. Such endpoints could be confusing or misleading if exposed in the API documentation. The @Hidden annotation allows you to keep these experimental features under the radar until they are ready for public exposure.

@Hidden
@PostMapping("/experimental/newFeature")
public ResponseEntity<?> testNewFeature() {
// Code for testing new feature
return new ResponseEntity<>(HttpStatus.OK);
}

Legacy or Deprecated Endpoints

In a long-lived application, some API endpoints may become deprecated or obsolete. Although it’s common to mark such routes as deprecated in the documentation, sometimes it’s better to remove them from the OpenAPI document altogether. This is especially true if you don’t want new clients to discover and start using them.

@Hidden
@GetMapping("/deprecated/oldFeature")
public OldFeature getOldFeature() {
// Fetch and return old feature data
}

Rate-limited or Resource-Intensive Endpoints

Some endpoints might perform resource-intensive operations or have a rate-limit to prevent abuse. While you could document the rate-limiting in the OpenAPI description, hiding such endpoints can be a more direct way of reducing misuse, especially if these routes are meant to be accessed by specific services or clients.

@Hidden
@GetMapping("/resourceIntensive/dataMining")
public ResourceData mineData() {
// Perform resource-intensive data mining
return new ResourceData();
}

Conditional Visibility

In some advanced scenarios, you might want the visibility of certain endpoints to be conditional based on runtime configurations or environment variables. Although this level of dynamic behavior can’t be directly achieved using @Hidden, you can programmatically modify the Springfox Docket configuration at runtime based on these conditions to achieve similar results.

@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(paths()) // Define a custom 'paths()' method with conditional logic
.build();
}

Drawbacks and Alternatives

The @Hidden annotation in Springfox offers an easy and convenient way to hide specific API endpoints from the generated OpenAPI documentation. While it brings many advantages, especially for complex systems with multiple services and APIs, it also has its share of limitations and drawbacks. Understanding these can help you make an informed decision on whether or not to employ @Hidden in your application, and what alternatives might be worth considering.

Vendor Lock-in

The @Hidden annotation is specific to the Springfox library. If your project is heavily reliant on this feature, migrating to another OpenAPI documentation library could be cumbersome, as you'd need to find an alternative mechanism for hiding endpoints.

Limited Dynamic Behavior

The @Hidden annotation is static by nature. You annotate a class or method once, and it remains hidden as long as the annotation is present. This can be limiting in scenarios where you'd want the visibility of an endpoint to be dynamic, based on runtime conditions or configurations.

Incomplete Security

While the @Hidden annotation prevents an API endpoint from appearing in the OpenAPI documentation, it does not actually disable or secure the endpoint. Users who are aware of the endpoint could still access it unless proper security measures are in place.

No Partial Information Hiding

The @Hidden annotation is an all-or-nothing proposition; you can't use it to hide specific details like request parameters or response models. For fine-grained control over the API documentation, you might need to look into more comprehensive methods.

Alternatives

Given these drawbacks, it’s essential to explore some alternatives.

Manual Docket Configuration

Springfox allows manual configuration of the Docket bean to specify which APIs or paths should be visible. This allows more dynamic behavior based on runtime conditions.

@Bean
public Docket customDocket() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.paths(Predicates.not(PathSelectors.regex("/hidden.*")))
.build();
}

Role-Based Access to Documentation

Some developers prefer to implement role-based access to the OpenAPI documentation itself, rather than hiding specific endpoints. This can be useful for internal APIs where different roles have different levels of access.

Custom Annotations

If @Hidden doesn't meet your needs, you can create custom annotations and leverage Springfox's OperationBuilderPlugin to customize API documentation generation based on your annotations.

@Component
public class CustomHiddenOperationBuilderPlugin implements OperationBuilderPlugin {
@Override
public void apply(OperationContext context) {
if (context.findAnnotation(CustomHidden.class).isPresent()) {
context.operationBuilder().hidden(true);
}
}

@Override
public boolean supports(DocumentationType delimiter) {
return DocumentationType.SWAGGER_2.equals(delimiter);
}
}

Use of API Gateways

In some scenarios, API Gateways can be used to control what portions of an API are exposed externally, thus replacing the need for the @Hidden annotation altogether.

Conclusion

Understanding and effectively leveraging the @Hidden annotation in Spring can bring substantial benefits, especially when dealing with complex API ecosystems. From controlling what should be visible in your OpenAPI documentation to maintaining the security and integrity of your APIs, this simple but powerful annotation offers a range of utilities. However, it's crucial to be aware of its limitations and the available alternatives, to make an informed choice tailored to the specific needs and constraints of your project. As with any tool, the key to getting the most out of the @Hidden annotation lies in understanding both its strengths and weaknesses.

  1. Springfox Official Documentation
  2. OpenAPI Specification
  3. Spring Security for API Protection
Spring Boot icon by Icons8

--

--

Alexander Obregon

Software Engineer, fervent coder & writer. Devoted to learning & assisting others. Connect on LinkedIn: https://www.linkedin.com/in/alexander-obregon-97849b229/