我正在尝试使用spring openapi codegen来生成API。我的期望是spring openapi generator应该生成一些默认模型和一个基本的未实现的控制器委托。这样我就可以将其实现到我自己的控制器层中,并根据我自己的需求使用它来公开业务逻辑。
现在我的问题是,我部分能够集成spring openapi codegen,因为我可以看到模型和接口成功生成,没有失败。
但问题是,当我在我的控制器类中实现自动生成的委托时,我的API并没有被公开,因此它们向我显示了一个404
错误。
此外,我也无法访问位于http://localhost:8956/swagger-ui/index.html
的swagger UI
请帮助我做错了什么pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.0.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.jainva.api</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<!-- <dependency>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-starter-data-jpa</artifactId>-->
<!-- </dependency>-->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>16.0.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/io.swagger.core.v3/swagger-annotations -->
<dependency>
<groupId>io.swagger.core.v3</groupId>
<artifactId>swagger-annotations</artifactId>
<version>2.2.8</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.annotation/javax.annotation-api -->
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.1.0.Final</version>
</dependency>
<!-- For rendering the spring UI-->
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<version>1.6.15</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
<version>3.0.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.openapitools/jackson-databind-nullable -->
<dependency>
<groupId>org.openapitools</groupId>
<artifactId>jackson-databind-nullable</artifactId>
<version>0.2.1</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<!-- RELEASE_VERSION -->
<version>6.0.0</version>
<!-- /RELEASE_VERSION -->
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>${project.basedir}/src/main/resources/api.yaml</inputSpec>
<generatorName>spring</generatorName>
<apiPackage>com.openapi.gen.springboot.api</apiPackage>
<modelPackage>com.openapi.gen.springboot.dto</modelPackage>
<supportingFilesToGenerate>
ApiUtil.java
</supportingFilesToGenerate>
<configOptions>
<delegatePattern>true</delegatePattern>
<serializableModel>true</serializableModel>
<sourceFolder>src/gen/java/main</sourceFolder>
<useTags>true</useTags>
</configOptions>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
我的控制器文件:
package com.jainva.api.controllers;
import com.jainva.api.http.models.CreateEmployeeRequestBody;
import com.jainva.api.services.EmployeServices;
import com.openapi.gen.springboot.api.EmployeesDataApiDelegate;
import com.openapi.gen.springboot.dto.EmployeesData;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.*;
@RestController
@Service
public class EmployeesController implements EmployeesDataApiDelegate {
private final EmployeServices empServices;
public EmployeesController(EmployeServices empServices) {
this.empServices = empServices;
}
@PostMapping("/api/v1/employee")
public void createEmployee(@RequestBody CreateEmployeeRequestBody body) {
try {
System.out.println("Post request to the server from controller layer");
empServices.createEmployee(body);
} catch (Exception e) {
ResponseEntity.internalServerError()
.body(e.getMessage());
}
}
@Override
public ResponseEntity<EmployeesData> getAllEmployeesData() {
System.out.println("Controller layer for get all employees");
try {
EmployeesData ed = new EmployeesData();
ed.setEmployees(empServices.getAllEmployees());
return ResponseEntity.ok().body(ed);
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
}
}
@GetMapping("api/v1/response")
public String response() {
return "Successful working server";
}
}
1条答案
按热度按时间vfhzx4xs1#
你是否激活了application.yml(www.example.com)中的springdocapplication.properties?
我在我的应用程序中使用.yml