Funsaized
Funsaized

Reputation: 2130

swagger-ui duplicating endpoints with interface and implementation of controller

I have a spring boot application. I have chosen to implement my controllers as an interface defining the endpoint and its respective implementation (i.e EndpointX, EndpointXController w/ EndpointXController being the implementation). I have all of my annotations for swagger in the interface files to prevent cluttering of the implementation class; however, I see duplicate endpoints on swagger UI as shown below:

enter image description here

This is my docket setup:

    @Bean
    public Docket customImplementation() {
        return new Docket(DocumentationType.SWAGGER_2)
            .select()
            .apis(RequestHandlerSelectors.withMethodAnnotation(RequestMapping.class))
            .paths(PathSelectors.ant("/consent/*"))
            .build()
            .directModelSubstitute(java.time.LocalDate.class, java.sql.Date.class)
            .directModelSubstitute(java.time.OffsetDateTime.class, java.util.Date.class)
            .apiInfo(apiInfo());
}

How can I tell swagger/swagger-ui to only show one endpoint for the rest service? I.e consent-api-controller wouldn't be shown or picked up by swagger.

Edit: Posted Controller and interface code

@Controller
public class ConsentApiController implements ConsentApi {

@Autowired
private IConsentApiService consentApiService;

private final ObjectMapper objectMapper;

private final HttpServletRequest request;

@Autowired
public ConsentApiController(ObjectMapper objectMapper, HttpServletRequest request) {
    this.objectMapper = objectMapper;
    this.request = request;
}

@Override
public Optional<ObjectMapper> getObjectMapper() {
    return Optional.ofNullable(objectMapper);
}

@Override
public Optional<HttpServletRequest> getRequest() {
    return Optional.ofNullable(request);
}

public ResponseEntity getConsent(@ApiParam(value = "Identifier for the consent object to be retrieved", required = true) @Valid @RequestBody ConsentReadRequestParent consentReadRequestParent) {
    return consentApiService.getConsent(consentReadRequestParent);
}

public ResponseEntity postConsent(@ApiParam(value = "Populated consent object") @Valid @RequestBody ConsentParentRequest consentObj) {
    // Pass request to service where it will be split into DTOs and passed to DAOs and get response
    return consentApiService.postConsent(consentObj);
}

public ResponseEntity searchConsent(@Valid @RequestBody SearchParentRequest spr){
    return consentApiService.searchConsent(spr);
}



}


@Api(value = "consent")
public interface ConsentApi {

default Optional<ObjectMapper> getObjectMapper() {
    return Optional.empty();
}

default Optional<HttpServletRequest> getRequest() {
    return Optional.empty();
}

default Optional<String> getAcceptHeader() {
    return getRequest().map(r -> r.getHeader("Accept"));
}

@ApiOperation(value = "The Read Consent API is a resource that conforms to a RESTful syntax to retrieve the details of a single consent record.", nickname = "getConsent", notes = "Cannot read without parameters. Minimum of 2 characters in each field. Maximum of 50 characters in each field. Should be able to handle special characters.", response = Consent.class, tags = {"consent",})
@ApiResponses(value = {
        @ApiResponse(code = 200, message = "OK", response = Consent.class),
        @ApiResponse(code = 400, message = "Bad Request"),
        @ApiResponse(code = 405, message = "Method Not Allowed"),
        @ApiResponse(code = 500, message = "Internal Server Error"),
        @ApiResponse(code = 604, message = "Could Not Retrieve Data for Consent"),
        @ApiResponse(code = 714, message = "Consent Not Found Matching Input Values")})
@RequestMapping(value = "/consent/read",
        method = RequestMethod.POST,
        consumes = {MediaType.APPLICATION_JSON_VALUE},
        produces = {MediaType.APPLICATION_JSON_VALUE})
ResponseEntity<?> getConsent(@ApiParam(value = "Identifier for the consent object to be retrieved.", required = true) @Valid @RequestBody ConsentReadRequestParent consentReadRequestParent);

@ApiOperation(value = "The Create Consent API is a resource that conforms to a RESTful syntax to persist a single consent record.", nickname = "postConsent", notes = "<business and backend logic/requirements>", response = ConsentResponseParent.class, tags = {"consent",})
@ApiResponses(value = {
        @ApiResponse(code = 200, message = "OK", response = ConsentResponseParent.class),
        @ApiResponse(code = 400, message = "Bad Request"),
        @ApiResponse(code = 405, message = "Method Not Allowed"),
        @ApiResponse(code = 500, message = "Internal Server Error")})
@RequestMapping(value = "/consent/create",
    method = RequestMethod.POST,
    consumes = {MediaType.APPLICATION_JSON_VALUE},
    produces = {MediaType.APPLICATION_JSON_VALUE})
ResponseEntity<?> postConsent(@ApiParam(value = "filled-out consent object")  @Valid @RequestBody ConsentParentRequest consentObj);

@ApiOperation(value = "The Search Consent API is a resource that conforms to a RESTful syntax to query consent records.", response = SearchParentResponse.class, tags = {"consent",})
@ApiResponses(value = {
        @ApiResponse(code = 200, message = "OK", response = SearchParentResponse.class),
        @ApiResponse(code = 400, message = "Bad Request"),
        @ApiResponse(code = 405, message = "Method Not Allowed"),
        @ApiResponse(code = 500, message = "Internal Server Error")})
@RequestMapping(value = "/consent/search",
        method = RequestMethod.POST,
        consumes = {MediaType.APPLICATION_JSON_VALUE},
        produces = {MediaType.APPLICATION_JSON_VALUE})
ResponseEntity<?> searchConsent(@Valid @RequestBody SearchParentRequest spr);

}

Upvotes: 2

Views: 4976

Answers (3)

Eduardo Briguenti Vieira
Eduardo Briguenti Vieira

Reputation: 4579

Another approach is jus the @Api for the controller class:

@Controller
@Api(tags = "consent")
public class ConsentApiController implements ConsentApi {

Or into the interface:

@Api(value = "consent", tags = "consent")
public interface ConsentApi {

Upvotes: 0

Arun K L
Arun K L

Reputation: 86

Remove attribute "tags" from @ApiOperation annotation. Adding tags will make the api appear under the context menu as well as under its own separate menu.

Upvotes: 5

Hopeinacup
Hopeinacup

Reputation: 1

You could explicitly exclude the controllers that should not be shown by moving the interfaces to a different package and using

.apis(RequestHandlerSelectors.basePackage("my.impl.package"))

or by writing your own, custom set of predicates and passing them into .apis().

However this would me more like a workaround. Are you certain there are no @RequestMapping annotations used in the implementing classes? I could not replicate this behaviour locally.

Upvotes: -1

Related Questions