Spring Boot Java Sping Boot SwaggerUI

l7mqbcuq  于 2023-05-17  发布在  Spring
关注(0)|答案(3)|浏览(160)

我在java spring Boot 上工作时遇到了一些问题。应用程序被编译,swagger ui启动,但它是空的。
下面是我的swagger配置:

package configuration;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@EnableSwagger2
@Configuration
public class SwaggerConfiguration {
    @Bean
    public Docket productApi()
    {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build()
                .apiInfo(metaInfo());
    }

    private ApiInfo metaInfo() {

        return new ApiInfoBuilder().title("Swagger for Demo app")
                                   .description("Demo application using swagger")
                                   .contact
                                   (
                                       new Contact
                                       (
                                           "Vastag Atila",
                                           "",
                                           "wasyster@gmail.com"
                                       )
                                   )
                                   .license("MIT")
                                   .version("1.0.0")
                                   .build();
    }
}

下面是我的控制器示例:

package controllers;

import common.ServiceObjectResponse;
import entity.Diak;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import request.RequestDiakByName;
import services.IDiakService;

import javax.validation.Valid;
import java.util.List;

@RestController
@Api
public class DiakController
{
    @Autowired
    IDiakService _diakService;

    @ApiOperation("getByname")
    @PostMapping("/api/diak/byname")
    @ResponseBody
    public Diak GetDiakByName(@RequestBody @Valid RequestDiakByName data) throws Exception
    {
        ServiceObjectResponse<Diak> request = _diakService.getByName(data.Name);

        if(!request.getIsSuccess())
        {
            throw new Exception(request.getMessage());
        }
        return request.getObject();
    } 
}

主应用程序:

package app;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@ComponentScan({"controllers", "service", "repository"})
public class WebApiApplication
{
    public static void main(String[] args)
    {
        SpringApplication.run(WebApiApplication.class, args);
    }
}

导入的依赖项:

<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>
    <scope>compile</scope>
</dependency>

我的项目是一个多Maven项目,如果它计数的东西。我遵循了许多教程,但仍然不能让它工作。
thnx

q0qdq0h2

q0qdq0h21#

变更

@ComponentScan({"controllers", "service", "repository"})

@ComponentScan({"controllers", "service", "repository", "configuration"})
eulz3vhy

eulz3vhy2#

我已经修改了你的代码为swagger配置。你可以试试

package configuration;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@EnableSwagger2
@Configuration
public class SwaggerConfiguration {
    @Bean
  public Docket api() {
    return new Docket(DocumentationType.SWAGGER_2)
        .select()
        .apis(RequestHandlerSelectors.basePackage("<your base package>"))
        .paths(PathSelectors.any())
        .build()
        .apiInfo(apiInfo());
  }

  private ApiInfo apiInfo() {
    return new ApiInfoBuilder()
        .title("Swagger for Demo app")
        .description("Demo application using swagger")
        .termsOfServiceUrl("http://springfox.io")
        .contact(
            new Contact(
                "DellEmc", "http://www.example.com/index.htm", "<some@emailid.com>"))
        .license("MIT"")
        .licenseUrl(
            "http://www.licenseurl.com/")
        .version("0.0.1")
        .build();
  }

}

我还修改了控制器类,如下所示。

@RestController
@Api(
    value = "IDiakService",
    description = "API for IDiakService",
    tags = {"IDiakService"})
public class DiakController
{
    @Autowired
    IDiakService _diakService;

    @ApiOperation(
      value = "Get Diak By Name",
      tags = {"IDiakService"})
    @PostMapping("/api/diak/byname")
    @ResponseBody
    public Diak GetDiakByName(@RequestBody @Valid RequestDiakByName data) throws Exception
    {
        ServiceObjectResponse<Diak> request = _diakService.getByName(data.Name);

        if(!request.getIsSuccess())
        {
            throw new Exception(request.getMessage());
        }
        return request.getObject();
    } 
} }

最后,您必须像这样提供组件扫描。

@ComponentScan(basePackages = { "com.app.module.*" }) // you have to specify the package name.
wz3gfoph

wz3gfoph3#

如果您使用Swagger和Sping Boot ,只需在您的主应用程序中添加@EnableSwagger2,并在配置中的bean下方添加,从而允许提及要扫描控制器API的包。

@Bean
public Docket productApi() {
    return new Docket(DocumentationType.SWAGGER_2).select()
            .apis(RequestHandlerSelectors.basePackage("com.your.package")).build();
}

相关问题