मैं इसे एक ग्रेडल आधारित वसंत बूट एप्लिकेशन के अंदर swagger2 कॉन्फ़िगरेशन के साथ उत्तर दे रहा हूं। Swagger2 के लिए आवश्यक विन्यास निम्नलिखित हैं।
जोड़े Gradle configuartion
जोड़े Gradle build.gradle फाइल के अंदर निर्भरता
dependencies {
compile("io.springfox:springfox-swagger2:2.0.2")
compile("io.springfox:springfox-swagger-ui:2.0.2")
}
Swagger2 Confugration कक्षा
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket userApi() {
return new Docket(DocumentationType.SWAGGER_2).select().apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any()).build().pathMapping("/")
.directModelSubstitute(LocalDate.class, String.class)
.genericModelSubstitutes(ResponseEntity.class)
.alternateTypeRules(newRule(
typeResolver.resolve(DeferredResult.class,
typeResolver.resolve(ResponseEntity.class, WildcardType.class)),
typeResolver.resolve(WildcardType.class)))
.useDefaultResponseMessages(false)
.globalResponseMessage(RequestMethod.GET,
newArrayList(new ResponseMessageBuilder().code(500).message("500 message")
.responseModel(new ModelRef("Error")).build()))
.securitySchemes(newArrayList(apiKey())).securityContexts(newArrayList(securityContext()))
.apiInfo(apiInfo());
}
@Autowired
private TypeResolver typeResolver;
private ApiKey apiKey() {
return new ApiKey("mykey", "api_key", "header");
}
private SecurityContext securityContext() {
return SecurityContext.builder().securityReferences(defaultAuth())
.forPaths(PathSelectors.regex("/anyPath.*")).build();
}
List<SecurityReference> defaultAuth() {
AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
authorizationScopes[0] = authorizationScope;
return newArrayList(new SecurityReference("mykey", authorizationScopes));
}
@Bean
SecurityConfiguration security() {
return new SecurityConfiguration("123456", "test-app-realm", "clientapp", "apiKey");
}
@Bean
UiConfiguration uiConfig() {
return new UiConfiguration("validatorUrl");
}
private ApiInfo apiInfo() {
ApiInfo apiInfo = new ApiInfo("DSM API", "API for DSM", "1.0.0", "termsOfServiceUrl",
"[email protected]", null, null);
return apiInfo;
}
}
जोड़े स्वैगर यूआई
गीथब से Swagger UI डाउनलोड करें।अपने src/मुख्य/संसाधनों में जिले फ़ोल्डर की प्रतिलिपि/स्थिर निर्देशिका और अकड़ को
HomeController.class जिले का नाम बदलने
@Api(basePath = "/", value = "/", description = "Home Controller")
@Controller
public class HomeController {
@RequestMapping("/")
public String home() {
return "redirect:swagger-ui.html";
}
}
MyApplication.class
@SpringBootApplication
@ComponentScan(basePackageClasses = SwaggerConfig.class)
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
निर्भरता के साथ अपने वसंत बूट एप्लिकेशन को कॉन्फ़िगर करें और टी देखने के लिए चलाएं वह एपीआई।
यूआरएल http://localhost:8080/v2/swagger-ui.html होगा आप इसे उपरोक्त उत्तर के रूप में भी अनुकूलित कर सकते हैं।
कृपया आप इस काम के मिली थी? मैं उस ब्लॉग पोस्ट का पालन नहीं कर रहा हूं क्योंकि चीजें बहुत बदल गई हैं, लेकिन मुझे यह काम नहीं मिल रहा है। अपडेट किए गए निर्देशों के लिए https://github.com/martypitt/swagger-springmvc देखें –
मेरे पास स्प्रिंगबूट के साथ ठीक से काम कर रहा है (हालांकि इसमें कुछ समय लगेगा!) आप कहां हैं और मैं कोशिश करूँगा और –
में मदद करूंगा लेकिन मूल कारण –