Swagger Codegen quick use

Swagger provides a number of tools to facilitate automatic code generation using Swagger definitions. The following are the steps to use Swagger Codegen to generate the client library:

  1. 下载Swagger Codegen(https://github.com/swagger-api/swagger-codegen/releases)。
  2. Copy the Swagger definitions to your local filesystem. Suppose your Swagger is defined in swagger.json file.
  3. Open a terminal and change to the directory of Swagger Codegen.
  4. Run the following command to generate the client library:
java -jar swagger-codegen-cli.jar generate -i swagger.json -l java -o ./

This command generates Java client code to the current directory.
5. If you want to specify the folder path for the generated code, use the --output option, for example:

java -jar swagger-codegen-cli.jar generate -i swagger.json -l java --output /path/to/generated/client/code
  1. Next, you can import the generated client code into your project and start using it.

In addition, Swagger Codegen also provides support for many other languages ​​and frameworks, and you can use different options in the command to generate these client libraries. For example, if you want to generate a Python client library, you can use the following command:

java -jar swagger-codegen-cli.jar generate -i swagger.json -l python -o ./

How Swagger Codegen customizes templates

Swagger Codegen is a very flexible tool that allows you to customize the generated code through templates. You can write custom code in templates and specify template files and template engines using command-line arguments.
Here is a simple example showing how to create custom templates using the Mustache template engine:

  1. Create the template file
    In any editable text editor, create a file and name it custom.mustache. Add the following to the file:
mustache
{
    
    {
    
    #imports}}
import {
    
    {
    
    .}};
{
    
    {
    
    /imports}}
 public class {
    
    {
    
    classname}} {
    
    
    {
    
    {
    
    #api}}
    public {
    
    {
    
    returnType}} {
    
    {
    
    nickname}}({
    
    {
    
    #required}}{
    
    {
    
    parameterName}} {
    
    {
    
    parameterName}}_{
    
    {
    
    #hasMore}},{
    
    {
    
    /hasMore}}{
    
    {
    
    /required}}) {
    
    
        // Add your custom code here
    }
    {
    
    {
    
    /api}}
}

The template contains basic code blocks for generating Java code and uses variables such as class names, imports, and API parameters. You can add or remove code blocks as needed.
2. Using a custom template
Use the following command on the command line to generate Java code based on your custom template:

swagger-codegen generate -i swagger.json -l java --api-package com.example.api --model-package com.example.model -o outputdir -t /path/to/templates

In this example, the -t parameter tells Swagger Codegen to use the template files in the /templates directory. You can replace the -t parameter with the appropriate path and replace /path/to/templates with the actual path.
When running the command, Swagger Codegen will generate Java code according to your custom template. You can edit templates to include code blocks specific to your project and apply them to generated code by specifying the template path.

Guess you like

Origin blog.csdn.net/weixin_43031220/article/details/130632148