question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Please add a constructor function for Docket.class

See original GitHub issue

Here is the reason: For now users can only register Docket using Spring @Bean annotation. And if you want to register the Docket by hand using BeanDefinition, you will fail because Docket creates instances using builder-mode, rather than the normal way (setter and getter). Spring context can not inject values for Docket’s member variables.

@dilipkrish

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
dilipkrishcommented, May 31, 2018

@tbwork that is what I would do as well. Thanks for the solution!

0reactions
tbworkcommented, Jul 27, 2020

This is simplely my method to optimize the usage of swagger in terms of convenience.

1. Define a new swagger definition annotation:

/**
 * <b>Please note</b> that one module is corresponding to one controller.
 * This annotation is used to define a module for a certain group of APIs.
 * 
 * @author tbwork
 */
@Target(value = {ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented   
@Inherited
public @interface SwaggerModule {

	/**
	 * The name of module. Please notice that the module names of different
	 * controllers should also differ from each other. The name will be shown
	 * in the <b>"spec" drop-list options</b> on the swagger-ui.html page. 
	 */
	public String name() default "";
	
	/**
	 * The summary title of APIs in this module.
	 */
	public String title() default "";
	
	/**
	 * The detailed description of this module.
	 */
	public String description() default "";
	
	/**
	 * The version of APIs.
	 */
	public String version() default "New" ;
	
	/**
	 * Tag this module with summary words so that users can find them easily 
	 * and friendly.
	 */
	public String [] tags() default {};
	
	/**
	 * The author or person in charge of this module. Please provider the full
	 * Chinese name so that others can contact you easily.
	 */
	public String author() default "stranger";
	
	/**
	 * The email of the author. Other users can contact the author by this email.
	 */
	public String email() default "";
	  
}

2. Providing an annotation as “SwaggerConfiguration”, and use it to specify one package path for searching those swagger modules. Following is only an example:

	private void collect() {
		List<Class<?>> docketClasses = getClassWithSpecifiedAnnotation(SwaggerModule.class);
		for (Class<?> klass : docketClasses) {
			SwaggerModule annotation = klass.getAnnotation(SwaggerModule.class); 
			addDocket(annotation.name(), annotation.title(), annotation.description(), 
					  annotation.version(), annotation.author(), annotation.email(), klass,
					  annotation.tags());
		}
	}

	private List<Class<?>> getClassWithSpecifiedAnnotation(Class<? extends Annotation> annotation){
		List<Class<?>> result = new ArrayList<Class<?>>();
		ClassPathScanningCandidateComponentProvider scanner = new ClassPathScanningCandidateComponentProvider(false);
        scanner.addIncludeFilter(new AnnotationTypeFilter(annotation));
        for (BeanDefinition bd : scanner.findCandidateComponents("xxx.xxxx")) {
        	try {
				result.add(Class.forName(bd.getBeanClassName()));
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				throw new RuntimeException(e.getMessage());
			}
        } 
        return result;
	}

Hope it is helpful. Have a nice day!

Read more comments on GitHub >

github_iconTop Results From Across the Web

The method apis(java.util.function.Predicate<springfox ...
I have added Swagger config class with @Beans as usual. The method apis(java.util.function.Predicate<springfox.documentation.RequestHandler>) in ...
Read more >
Spring Boot Swagger 2 Configuration Error creating bean with ...
Hi, I am running into an unsatisfied dependency exception for apiModelReader class when trying to configure swagger support for my spring web ...
Read more >
Class Docket
A builder which is intended to be the primary interface into the Springfox framework. Provides sensible defaults and convenience methods for configuration.
Read more >
How to configure Swagger in Spring Boot - Brain Bytes
We'll create a Docket instance and customize the API metadata and the details. We'll also use various annotations to add notes and ...
Read more >
Advanced Swagger Configuration with Spring Boot
Customizing the Docket Bean ... As you can see in the above code, we have added apiInfo() method that expects an object of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found