我在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
3条答案
按热度按时间q0qdq0h21#
变更
到
eulz3vhy2#
我已经修改了你的代码为swagger配置。你可以试试
我还修改了控制器类,如下所示。
最后,您必须像这样提供组件扫描。
wz3gfoph3#
如果您使用Swagger和Sping Boot ,只需在您的主应用程序中添加@EnableSwagger2,并在配置中的bean下方添加,从而允许提及要扫描控制器API的包。