Expand Authors Top

If you have a few years of experience in the Java ecosystem and you’d like to share that with the community, have a look at our Contribution Guidelines.

November Discount Launch 2022 – Top
We’re finally running a Black Friday launch. All Courses are 30% off until next Friday:

>> GET ACCESS NOW

November Discount Launch 2022 – TEMP TOP (NPI)
We’re finally running a Black Friday launch. All Courses are 30% off until next Friday:

>> GET ACCESS NOW

Expanded Audience – Frontegg – Security (partner)
announcement - icon User management is very complex, when implemented properly. No surprise here.

Not having to roll all of that out manually, but instead integrating a mature, fully-fledged solution - yeah, that makes a lot of sense.
That's basically what Frontegg is - User Management for your application. It's focused on making your app scalable, secure and enjoyable for your users.
From signup to authentication, it supports simple scenarios all the way to complex and custom application logic.

Have a look:

>> Elegant User Management, Tailor-made for B2B SaaS

1. Overview

While creating Swagger documentation, we often need to hide endpoints from being exposed to end-users. The most common scenario to do so is when an endpoint is not ready yet. Also, we could have some private endpoints which we don't want to expose.

In this short article, we'll have a look at how we can hide endpoints from Swagger API documentation. To achieve this, we'll be using annotations in our controller class.

2. Hiding an Endpoint with @ApiIgnore

The @ApiIgnore annotation allows us to hide an endpoint. Let's add this annotation for an endpoint in our controller:

@ApiIgnore
@ApiOperation(value = "This method is used to get the author name.")
@GetMapping("/getAuthor")
public String getAuthor() {
    return "Umang Budhwar";
}

3. Hiding an Endpoint with @ApiOperation

Alternatively, we can use @ApiOperation to hide a single endpoint:

@ApiOperation(value = "This method is used to get the current date.", hidden = true)
@GetMapping("/getDate")
public LocalDate getDate() {
    return LocalDate.now();
}

Notice that we need to set the hidden property to true to make Swagger ignore this endpoint.

4. Hiding All Endpoints with @ApiIgnore

Nonetheless, sometimes we need to hide all the endpoints of a controller class. We can achieve this by annotating the controller class with @ApiIgnore:

@ApiIgnore
@RestController
public class RegularRestController {
    // regular code
}

It is to be noted that this will hide the controller itself from the documentation.

6. Hiding an Endpoint with @Hidden

If we're using OpenAPI v3, we can hide an endpoint using the @Hidden annotation:

@Hidden
@GetMapping("/getAuthor")
public String getAuthor() {
    return "Umang Budhwar";
}

7. Hiding All Endpoints with @Hidden

Similarly, we can annotate the controller with @Hidden to hide all the endpoints:

@Hidden
@RestController
public class RegularRestController {
    // regular code
}

This will also hide the controller from the documentation.

Note: We can only use @Hidden when we're using OpenAPI. The support for this annotation in Swagger v3 is still in progress.

8. Conclusion

In this tutorial, we've seen how we can hide the endpoints from Swagger documentation. We discussed how to hide a single endpoint and also all the endpoints of a controller class.

As always, the complete code for the Swagger examples is available over on GitHub, and the OpenAPI v3 examples are available over on GitHub as well.

November Discount Launch 2022 – Bottom
We’re finally running a Black Friday launch. All Courses are 30% off until next Friday:

>> GET ACCESS NOW

REST footer banner
2 Comments
Oldest
Newest
Inline Feedbacks
View all comments
Comments are closed on this article!