Java 如何在项目目录中包含swagger生成的类
我正在尝试使用Swagger Codegen为我的spring boot项目生成模型类。我在网上找到了一些参考资料,并在我的pom.xml中包含了以下插件:Java 如何在项目目录中包含swagger生成的类,java,spring-boot,maven,swagger,swagger-codegen,Java,Spring Boot,Maven,Swagger,Swagger Codegen,我正在尝试使用Swagger Codegen为我的spring boot项目生成模型类。我在网上找到了一些参考资料,并在我的pom.xml中包含了以下插件: <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>
${project.basedir}/src/main/resources/Contract-v1.yml
</inputSpec>
<generatorName>spring</generatorName>
<apiPackage>${project.groupId}.swagger.api</apiPackage>
<modelPackage>${project.groupId}.swagger.model</modelPackage>
<supportingFilesToGenerate>
ApiUtil.java
</supportingFilesToGenerate>
<configOptions>
<sourceFolder>src/main/java/</sourceFolder>
<delegatePattern>true</delegatePattern>
<interfaceOnly>true</interfaceOnly>
</configOptions>
</configuration>
</execution>
</executions>
</plugin>
org.springframework.boot
springbootmaven插件
org.openapitools
openapi生成器maven插件
生成
${project.basedir}/src/main/resources/Contract-v1.yml
春天
${project.groupId}.swagger.api
${project.groupId}.swagger.model
ApiUtil.java
src/main/java/
真的
真的
我运行mvn install
并在目录/target/generated sources/openapi
中生成类。但我无法在REST控制器类中导入这些生成的类
我的理解是,
字段用于标识生成的类必须放置在其中的包。我说的对吗
即使这些生成的类在正确的包中,因为它们不在src/main/java
中,我可能无法将它们导入其他类中
有没有办法将这些生成的类放在
src/main/java
目录下,或者我在maven配置中遗漏了一些东西,因为这些文件对其他类不可用?对于swagger UI,您需要提供依赖项,需要进行一些基本配置,并且在REST API中,您需要包含两个变化
您可以引用使用Swagger创建的此API
在Spring引导中,主应用程序文件包括以下代码
@SpringBootApplication
@EnableSwagger2
public class BlogappApplication {
public static void main(String[] args) {
SpringApplication.run(BlogappApplication.class, args);
}
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2).select()
.apis(RequestHandlerSelectors
.basePackage("com.example.blogapp.controller"))
.paths(PathSelectors.regex("/.*"))
.build().apiInfo(apiEndPointsInfo());
}
private ApiInfo apiEndPointsInfo() {
return new ApiInfoBuilder().title("Spring Boot REST API")
.description("Blog Application REST API")
.contact(new Contact("name", "url", "email id"))
.build();
}
}
并修改每个控制器,如下面的代码所示
@ApiOperation(value = "Deleting a comment", response = String.class)
@ApiParam(value = "Comment ID", required = true)
@ApiImplicitParam(name = "Authorization", value = "Access Token", required = true, allowEmptyValue = false, paramType = "header", dataTypeClass = String.class, example = "Bearer access_token")
@DeleteMapping
public String delete(@RequestParam String id){
return commentService.delete(id);
}
在使用这些代码之前,请浏览我为swagger UI提供的heroku swagger链接,您需要提供依赖项,并需要进行一些基本配置,在REST API中还需要包括一些更改 您可以引用使用Swagger创建的此API 在Spring引导中,主应用程序文件包括以下代码
@SpringBootApplication
@EnableSwagger2
public class BlogappApplication {
public static void main(String[] args) {
SpringApplication.run(BlogappApplication.class, args);
}
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2).select()
.apis(RequestHandlerSelectors
.basePackage("com.example.blogapp.controller"))
.paths(PathSelectors.regex("/.*"))
.build().apiInfo(apiEndPointsInfo());
}
private ApiInfo apiEndPointsInfo() {
return new ApiInfoBuilder().title("Spring Boot REST API")
.description("Blog Application REST API")
.contact(new Contact("name", "url", "email id"))
.build();
}
}
并修改每个控制器,如下面的代码所示
@ApiOperation(value = "Deleting a comment", response = String.class)
@ApiParam(value = "Comment ID", required = true)
@ApiImplicitParam(name = "Authorization", value = "Access Token", required = true, allowEmptyValue = false, paramType = "header", dataTypeClass = String.class, example = "Bearer access_token")
@DeleteMapping
public String delete(@RequestParam String id){
return commentService.delete(id);
}
在使用代码之前,请浏览我提供的heroku swagger链接,您已经解释了如何为项目配置swagger UI。我正在研究如何使用swagger code gen,您已经解释了如何为项目配置swagger UI。我正在研究如何使用招摇过市代码gen,