Reputation: 2505
I am having some problems to get to work swagger on java spring boot. The app gets compiled, the swagger ui starts, but it is empty.
Here is my swagger config:
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",
"",
"[email protected]"
)
)
.license("MIT")
.version("1.0.0")
.build();
}
}
Here is my controller example:
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();
}
}
And the main app:
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);
}
}
The imported dependencies:
<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>
My project is a multy maven project, if it counts something. I follows many tutorials, but still can't get it to work.
thnx
Upvotes: 0
Views: 436
Reputation: 3
If you use Swagger with Spring Boot, just add @EnableSwagger2 in your main application and below bean in the configuration which allows mentioning packages to be scanned for controller APIs.
@Bean
public Docket productApi() {
return new Docket(DocumentationType.SWAGGER_2).select()
.apis(RequestHandlerSelectors.basePackage("com.your.package")).build();
}
Upvotes: 0
Reputation: 8001
I have modified your code for swagger configuration. You can try.
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", "<[email protected]>"))
.license("MIT"")
.licenseUrl(
"http://www.licenseurl.com/")
.version("0.0.1")
.build();
}
}
I have also modified controller class like below.
@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();
}
} }
Finally, you have to provide the component scan like this.
@ComponentScan(basePackages = { "com.app.module.*" }) // you have to specify the package name.
Upvotes: 0
Reputation: 6537
Change
@ComponentScan({"controllers", "service", "repository"})
to
@ComponentScan({"controllers", "service", "repository", "configuration"})
Upvotes: 1