Search code examples
javaspring-bootswagger-codegen

Reference to the target-file. Why/How is this created and used?


I recently started out with Spring-Security and in this context found a project on GitHub matching my interests.

While reading some code i discovered that a class (facade\impl\UserFacadeImpl.java) was linked to the target package. This means, when i run

  • mvn package: the target-file is created and the import-link is valid
  • mvn clean: the target-file is destroyed and my IDE marks the import-link as invalid

Imports from the target-directory and the class being used normally:

import com.boza.swaggergen.model.Credential;
import com.boza.swaggergen.model.User;

public class UserFacadeImpl implements UserFacade {

    @Override
    public User createUser(final User user) {
        UserModel userModel = modelMapper.map(user, UserModel.class);
        userModel = userService.createUser(userModel);
        return modelMapper.map(userModel, User.class);
    }

The UserModel class shares the same fields with use User class, but the methods are different.

I have never seen anything like this and am completly baffled. I looked in the configuration files, but couldn`t find a hint where those classes are generated.


Solution

  • Those class are generated by Swagger Codegen. The general workflow is:

    1. Describe API using OpenAPI spec.
    2. Configure Maven 's POM to use swagger-codegen-maven-plugin to generate the codes.
    3. Generate codes by mvn generate-sources (mvn package will call it behind scene)
    4. It only generates an abstract @RestController that is configured with @RequestMapping and the request/response POJO for each API endpoint. You still have to implement the actual logic by extending the generated @RestController.