Swagger - how to use swagger-codegen and build a project in the long run

The Swagger project is great for easily creating client and server applications in any language, but I don’t understand how to properly maintain or extend an existing project.

I am creating a Java server - the language setting is jaxrs-spec.

I wrote my own Mavenpom and swagger-codegen-maven-pluginam using it and now I can generate the model or API directly using Maven.

I see that every time I rebuild my project, the API is completely rewritten by the plugin swagger-codegen.

I need to implement the application logic, and at the moment I have not seen any other way but to configure the generated API sources. Thus, in order to avoid loss of work with each assembly, I could configure .swagger-codegen-ignore(like me), but I only postponed the problem.

Because the first time I need to update swagger.json, the only option I have is to restore the entire API and overwrite my work. Ok, I could use git and try to recover the deleted part, but this is a really terrible solution.

So the question is:

Using Swagger swagger-codegen(or Swagger in general) Is there a template, like, a documented way that allows me to add behavior and / or extend the generated APIs without overwriting my code?

+7
1

/, , , : . , jaxrs-spec, , , jaxrs-resteasy.

:

  • jaxrs
  • / swagger-codegen .

, . , , , . Codgen , API, .

jaxrs-resteasy, GET /{username} ( swagger, ):

@GET
@Path("/{username}")
@Produces({ "application/xml", "application/json" })
public Response getUserByName( @PathParam("username") String username,@Context SecurityContext securityContext)
throws NotFoundException {
    return delegate.getUserByName(username,securityContext);
}

factory, :

private final UserApiService delegate = UserApiServiceFactory.getUserApi();

, API, :

public abstract class UserApiService {
    // methods omitted...
    public abstract Response getUserByName(String username, SecurityContext securityContext) throws NotFoundException;
    // methods omitted...
}

, , :

public class UserApiServiceImpl extends UserApiService {
      // methods omitted...
      @Override
      public Response getUserByName(String username, SecurityContextsecurityContext) throws NotFoundException {
          // do some magic!
          return Response.ok().entity(new ApiResponseMessage(ApiResponseMessage.OK, "magic!")).build();
    }
    // methods omitted...
}

factory , , :

public class UserApiServiceFactory {
   private final static UserApiService service = new UserApiServiceImpl();

   public static UserApiService getUserApi() {
      return service;
   }
}

, , , UserApiService . , API .

, jaxrs-spec , swagger-codegen . .

+7

Source: https://habr.com/ru/post/1667978/


All Articles