Generate UI model from Java -> Swagger

Viewed 62
  • I can use

        <dependency>
            <groupId>io.swagger.codegen.v3</groupId>
            <artifactId>swagger-codegen-maven-plugin</artifactId>
            <version>${swagger-codegen-maven-plugin.version}</version>
        </dependency>
    

    to create a UI (TypeScript) and back end (Java) models from the swagger-ui.yml file (manually written) - I do not want Swagger to generate endpoints (I want to write them in Java).

  • I can also access a new Swagger file at runtime with springfox-swagger2 + springfox-swagger-ui (2.9.2)

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>
    

I want to write everything in Java (models and endpoints), and then somehow generate swagger-ui.yml from Java, and then generate TypeScript models from that swagger-ui.yml file (like I can with swagger.codegen), though I was unsuccessful with combining these two approaches. Or is there some other way (other dependencies, etc.) to do this? I also need the Swagger file to include endpoints documentation, like using springfox-swagger-ui. (I am using Maven, spring-boot-starter-web, Java 8, Angular 13.)

1 Answers

The idea is that Swagger will give you the "API" endpoints (as interfaces) and you will write the "API implementation" in Java.

For example, from your swagger-ui.yml file:

Your MyappApi class might look like this:

@Api(description = "the myapp API")
@javax.annotation.Generated(...)public interface MyappApi {
    // ... Endpoints go here
}

And then you write MyappApiImpl like this:

public class MyappApiImpl implements MyappApi {
    // ... Override methods here
}

Note that it’s very important the TypeScript classes and Java API endpoints are kept in sync. You do not want to handwrite everything in Java - let Swagger define the API implementation (server side, for Java) and you define the implementations only.

Related