Search code examples
javaspringswagger-uiswagger-codegen

Springfox duplicating controllers


I am trying to work with both swagger-codegen and springfox to gain time during webservice development.

I am facing an issue with an endpoint being created for my annotated interface classes as well as my controller implementations as you can see below:

I found a workaround by adding the tag where the controller should be (ex: @Api(tags={ "Player" })) in my controller but I am looking for a better one preventing this because if I am using code generation its to avoid this kind of situation where you have to add stuff in your code.

With swagger-codegen, I just have to write a RestController (PlayerApiImpl) like this:

@RestController
public class PlayerApiImpl implements PlayerApi {

    @Override
    public ResponseEntity<Player> playerIdGet(String id) {
        PlayerDTO ret = service.getOne(Long.parseLong(id));
        if (ret == null) {
            throw  new PlayerNotFoundException();
        }
        return ResponseEntity.ok(mapper.toModel(ret));
    }
}

While everything is generated into an interface (here PlayerApi). So I would like to stay as simple as it is possible.


Solution

  • I decided to push my workaround a bit more and found this solution:

    It's actualy possible de add the @Api annotation directly in the generated interface to prevent you to add it in your implementation. For that you need to work with mustache templates files that you can create in the directory where is located your swagger file.

    It might not be the best solution, so if you have a better proposolal, feel free to comment.