Java swagger private and public documentation - java

Is there a way to make swagger private documentation? I have documentation that needs to be accessible and public and some "admin" methods just for my colleagues. Does the swagger have such functionality?
pom.xml
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>3.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
SpringFoxConfig.java
#Bean
public Docket admin() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.paths(PathSelectors.ant("/admin/**"))
.apis(RequestHandlerSelectors.basePackage("mypackage"))
.build()
.groupName("admin")
.tags(new Tag(USER, "User controller"))
.apiInfo(apiDetails())
.useDefaultResponseMessages(false);
}
#Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.paths(PathSelectors.ant("/api/**"))
.apis(RequestHandlerSelectors.basePackage("my package"))
.build()
.groupName("api")
.tags(new Tag(USER, "User controller"))
.apiInfo(apiDetails())
.useDefaultResponseMessages(false);
}
So I want my admin endpoints be accessible only for admins.
I do not use .yaml file for my docs, just annotating methods.

Related

How to add Swagger-UI to a project running with tomcat 9.0.41 and Spring, without Spring Boot

Has anyone tried adding Swagger-UI to projects running with Tomcat and Spring, but without Spring Boot?
My project runs on a Tomcat server, with spring. I want to add swagger-UI to see all the endpoints but I can't implement it.
I tried with all kinds of dependencies and configurations but I didn't manage to finish it.
This is my configuration:
#Configuration
#EnableWebMvc
#ComponentScan(basePackages = {"com.betfair"})
#Import(SwaggerConfig.class)
public class AppConfig extends WebMvcConfigurerAdapter {
#Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new LocaleChangeInterceptor());
}
#Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
}
}
#Configuration
#EnableSwagger2
public class SwaggerConfig {
#Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2).select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build()
.apiInfo(apiInfo());
}
private ApiInfo apiInfo() {
ApiInfo apiInfo = new ApiInfo("My REST API", "Some custom description of API.", "API TOS", "Terms of service", "myeaddress#company.com", "License of API", "API license URL");
return apiInfo;
} }
#Configuration
#EnableWebSecurity
public class WebSecurityConfiguration implements WebSecurityCustomizer {
#Override
public void customize(WebSecurity web) {
web.ignoring().antMatchers("/v2/api-docs",
"/configuration/ui",
"/swagger-resources/**",
"/configuration/security",
"/swagger-ui.html",
"/webjars/**");
}
}
// My pom.xml dependencies:
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-schema</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>5.7.2</version>
</dependency>
What I am missing? Do I need more special configurations? Thanks.

Swagger API Tag Description Not Coming

I am using below swagger maven depedepncy.
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.8.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.8.0</version>
<scope>compile</scope>
</dependency>
Config
public Docket productApi() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("com.it"))
.build()
.tags(new Tag("Admin API", "Admin interface to manage users"));
}
Controller
#RequestMapping(value = "/kyc")
#Api(tags = {"Admin API"})
#SwaggerDefinition(tags = {
#Tag(name = "Admin API", description = "Admin interface to manage users")
})
public class KycController
But in swagger-ui, description of the Tag is not coming as Admin interface to manage users

Swagger with spring MVC not generating the documentation of the services

i am using swagger with spring mvc, the pom.xml
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.6.1</version>
</dependency>
and in spring configuration.xml
<mvc:resources mapping="swagger-ui.html" location="classpath:/META-INF/resources/" />
<mvc:resources mapping="/webjars/**"
location="classpath:/META-INF/resources/webjars/" />
also i use java-based configuration
#Configuration
#EnableSwagger2
#PropertySource("classpath:application.properties")
public class SwaggerConfig extends WebMvcConfigurerAdapter {
#Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2).select().apis(RequestHandlerSelectors.any())
.paths(PathSelectors.regex("/api/.*")).build().apiInfo(apiInfo());
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder().title(" Admin Api").description("Admin Api").version("V1")
.termsOfServiceUrl("http://terms-of-services.url").license("LICENSE")
.licenseUrl("http://url-to-license.com").build();
}
}
in the controller i use
#Controller
#RequestMapping({ "/" })
#Api(value = "product", description = "Products Web Services") // Swagger annotation
public class ProductController {
#ApiOperation(value = "products", nickname = "Get list of all products", response = ProductListResponse.class)
#RequestMapping(value = "/products", method = RequestMethod.GET)
public #ResponseBody ProductListResponse getAllProducts(HttpServletResponse httpServletResponse) {
in application.propeties i added springfox.documentation.swagger.v2.path=/api-docs.
that is all information i used to generate documentation
using the url http://localhost:8080/admin-api/admin/api-docs , it doesnt generate documentation for the end points
{
swagger: "2.0",
info: {
description: " Admin Api",
version: "V1",
title: "Admin Api",
termsOfService: "http://terms-of-services.url",
license: {
name: "LICENSE",
url: "http://url-to-license.com"
}
},
host: "localhost:8080",
basePath: "/admin-api/admin"
}
Solved. after miss around many things i found the solution, i changed the method Docket api() to be
#Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2).select().apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any()).build().apiInfo(apiInfo());
}
i think the the part PathSelectors.regex("/api/.*") on the old method was restricting the lookup process of the end points

Spring Swagger-ui integration

Needless to say I know there are a ton of questions about this same subject but I've been stuck trying to fix this for 2 days now and most of what I've read is outdated.
So yeah.. this is what I have right now:
Gradle:
dependencies {
compile 'org.springframework.cloud:spring-cloud-starter-hystrix'
compile 'org.springframework.boot:spring-boot-starter-web'
compile 'org.springframework.boot:spring-boot-starter-actuator'
compile 'org.springframework.boot:spring-boot-starter-security'
compile 'org.springframework.ws:spring-ws-core'
compile 'io.springfox:springfox-swagger2:2.4.0'
compile 'io.springfox:springfox-swagger-ui:2.4.0'
}
Main class:
#EnableSwagger2
public class Application {
public static void main(String[] args) {
SpringApplication.run(FeedServiceApplication.class, args);
}
#Bean
public Docket swaggerSettings() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build()
.pathMapping("/");
}
When I visit: http://localhost:8080/v2/api-docs, I get the json documentation just fine. Also when I download swagger-ui from github, set the source to the link above and run it on desktop, it also works fine. What doesn't work is putting these 2 things together (getting it to work at http://localhost:8080/swagger-ui.html).
There are many tutorials such as these, where they claim the stuff above will make swagger-ui work:
http://kubecloud.io/guide-using-swagger-for-documenting-your-spring-boot-rest-api/
http://www.baeldung.com/swagger-2-documentation-for-spring-rest-api
And a ton of other tutorials where they instruct you to add dist folder from swagger-ui git to your project.
Also mapping like this:
#Configuration
#EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {
#Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("**/swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("**/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
}
}
Fails as well, throwing Scope 'request' is not active for the current thread; exception.
After trying some tutorials from youtube, the ones linked above and many others, all I've seen is "page not found". If anyone could explain what I'm missing I would be very grateful.
TL:DR How do I get swagger-ui.html to work?
EDIT: found the solution.
In case anyone else stumbles upon this, the problem is that if you have a request mapping that takes in a parameter #RequestMapping("/{param}"), dispatcherServlet no longer maps /** to ResourceHttpRequestHandler. The code below fixes that issue.
#Configuration
#EnableAutoConfiguration
#EnableSwagger2
public class SwaggerConfig extends WebMvcConfigurerAdapter{
#Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any()).build();
}
#Override public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
}
}
That's what we use as setup. A class for its own annotated with #Configuration
#Configuration
#EnableSwagger2
public class SwaggerConfig {
#Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(Predicates.not(PathSelectors.regex("/error")))
.build()
.apiInfo(apiInfo());
}
private ApiInfo apiInfo() {
return new ApiInfo(
"APP NAME",
"APP DESCRIPTION",
"1.0",
null,
new Contact("Firstname Lastname", null, "firstname.lastname#somewhere.net"),
null,
null);
}
}
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${swagger.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${swagger.version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-spi</artifactId>
<version>${swagger.version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-core</artifactId>
<version>${swagger.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-spring-web -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-spring-web</artifactId>
<version>${swagger.version}</version>
</dependency>
Convert the above dependency to gradle.
Version i used is 2.3.1
package XXXX;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
#EnableSwagger2
public class SwaggerConfiguration {
}
We enabled the Swagger Config as stated above.
You can add Docket bean for custom headers:
#Bean
public Docket docket() {
Parameter parameterAuthorization =
new ParameterBuilder().name("Authorization").description("Authentication of the API User")
.modelRef(new ModelRef("string")).parameterType("header").required(true).build();
Parameter parameterClientUserId =
new ParameterBuilder().name("user_id").description("Client user identifier")
.modelRef(new ModelRef("string")).parameterType("header").required(true).build();
return new Docket(DocumentationType.SWAGGER_2).select().apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any()).build()
.globalOperationParameters(Lists.newArrayList(parameterClientUserId, parameterAuthorization));
}
And finally import the Swagger config in Main Application Class on Spring boot
#Import({SwaggerConfiguration.class})

Arquillian Persistence Extension with Spring and Javaconfig

I am trying to use arquillian-persistence-dbunit where I have to seed the database with datas but none of my solutions work. My project is based on Spring and we are using JavaConfig.
In my POM, I have the arquillian jars :
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.jboss.arquillian</groupId>
<artifactId>arquillian-bom</artifactId>
<version>1.1.10.Final</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>
<!-- Arquillian dependencies -->
<dependency>
<groupId>org.jboss.arquillian.extension</groupId>
<artifactId>arquillian-persistence-dbunit</artifactId>
<version>1.0.0.Alpha7</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.arquillian.junit</groupId>
<artifactId>arquillian-junit-container</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.arquillian.extension</groupId>
<artifactId>arquillian-service-integration-spring-inject</artifactId>
<version>1.1.0.Alpha1</version>
</dependency>
<dependency>
<groupId>org.jboss.arquillian.extension</groupId>
<artifactId>arquillian-service-integration-spring-javaconfig</artifactId>
<version>1.1.0.Alpha1</version>
</dependency>
My test class :
#RunWith(Arquillian.class)
#Transactional(PersistanceTestConfig.TRANSACTION_NAME)
#SpringAnnotationConfiguration(classes = {ArquillianConfiguration.class})
#DataSource(PersistanceTestConfig.MY_DATASOURCE)
#UsingDataSet({"database/myDataset.yml"})
public class MyTestClass{
#Deployment(testable = false)
public static WebArchive createArchive(){
//returns a webArchive
}
#Test
public void myTestMethod(){
//do something
}
}
My config classes :
#Configuration
#Import({PersistanceTestConfig.class})
#ComponentScan(basePackages = {"com.myapp.dao", ""com.myapp.services"})
#EnableCaching
#EnableTransactionManagement
public class ArquillianConfiguration {}
And the PersistenceTestConfig :
public class PersistanceTestConfig {
public static final String TRANSACTION_NAME = "transaction";
public static final String MY_DATASOURCE = "datasource";
private Properties getJPAProperties() {
final Properties jpaProperties = new Properties();
jpaProperties.setProperty("hibernate.hbm2ddl.auto", "create");
jpaProperties.setProperty("hibernate.show_sql", "true");
jpaProperties.setProperty("hibernate.format_sql", "true");
jpaProperties.setProperty("hibernate.dialect", "org.hibernate.dialect.HSQLDialect");
return jpaProperties;
}
#Bean(name = MY_DATASOURCE)
public DataSource getDataSource(){
return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.HSQL)
.continueOnError(false).ignoreFailedDrops(true).build();
}
#Bean
#Autowired
public LocalContainerEntityManagerFactoryBean getEntityManagerREFCS( DataSource dataSource) {
final LocalContainerEntityManagerFactoryBean localContainerEntityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
localContainerEntityManagerFactoryBean.setDataSource(dataSource);
localContainerEntityManagerFactoryBean.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
localContainerEntityManagerFactoryBean.setPackagesToScan("com.myapp.dao.beans");
localContainerEntityManagerFactoryBean.setPersistenceUnitName("pUnitName");
localContainerEntityManagerFactoryBean.setJpaProperties(getJPAProperties());
return localContainerEntityManagerFactoryBean;
}
}
I have tried many solutions, but I still have the same error :
org.jboss.arquillian.transaction.impl.lifecycle.TransactionProviderNotFoundException: Transaction provider for given test case has not been found.
What bothers me is there are not so many documentations on how to use arquillian-persistence-dbunit with JavaConfig. The only example that I see are badsed on xml configuration only. For instance, in this project.
Has Anyone used arquillian-persistence-dbunit with JavaConfig.

Categories

Resources