2014-05-20 20 views
6

के साथ स्वैगर यूआई प्राप्त करने में सक्षम नहीं है मैं http://raibledesigns.com/rd/entry/documenting_your_spring_api_with पर आलेख का अनुसरण कर रहा हूं।वसंत बूट

सबकुछ ठीक काम करता है लेकिन स्वैगर यूआई को एकीकृत करने में सक्षम नहीं है।

http://localhost:8080/docs/index.html 

परिणाम/त्रुटि पुनर्निर्देशन में परिणाम।

+0

कृपया आप इस काम के मिली थी? मैं उस ब्लॉग पोस्ट का पालन नहीं कर रहा हूं क्योंकि चीजें बहुत बदल गई हैं, लेकिन मुझे यह काम नहीं मिल रहा है। अपडेट किए गए निर्देशों के लिए https://github.com/martypitt/swagger-springmvc देखें –

+0

मेरे पास स्प्रिंगबूट के साथ ठीक से काम कर रहा है (हालांकि इसमें कुछ समय लगेगा!) आप कहां हैं और मैं कोशिश करूँगा और –

+0

में मदद करूंगा लेकिन मूल कारण –

उत्तर

2

मुझे पता है कि यह एक पुराना सवाल है लेकिन शायद यह भविष्य में इसी तरह की समस्या वाले किसी की मदद करेगा।

मैंने आपके द्वारा उल्लेखित एक ही ट्यूटोरियल का पालन किया और मैंने इसे बिना किसी समस्या के काम किया। मैंने अपने स्वयं के दस्तावेज़ को हफ्ते पहले स्प्रिंग बूट प्रोजेक्ट जोड़े में UI के साथ एक स्वैगर सेट अप करने के तरीके पर रखा था। शायद यह आपकी मदद करेगा क्योंकि यह छोटा और अधिक अद्यतित है।

Maven निर्भरता जोड़े

स्टिक इन अपने pom.xml में:

<dependency> 
<groupId>com.mangofactory</groupId> 
<artifactId>swagger-springmvc</artifactId> 
<version>1.0.2</version> 
<type>jar</type> 
</dependency> 

जोड़े स्वैगर यूआई

GitHub से डाउनलोड करें Swagger UIdist फ़ोल्डर को अपनी webapp निर्देशिका में कॉपी करें और dist पर swagger (या कोई भी नाम जो आपको पसंद है) का नाम बदलें।

ओपन की नकल की निर्देशिका के अंदर index.html फ़ाइल और पहले जावास्क्रिप्ट समारोह में यूआरएल को बदलने तो यह /api-docs endpoint को इंगित करता है:

var url = window.location.search.match(/url=([^&]+)/); 
    if (url && url.length > 1) { 
    url = decodeURIComponent(url[1]); 
    } else { 
    url = "/project-name/api-docs"; 
} 

कॉन्फ़िगर स्वैगर

एक बनाएं SwaggerConfig.java कक्षा और वहां swagger कॉन्फ़िगर करें:

@Configuration 
@EnableSwagger 
@EnableAutoConfiguration 
public class SwaggerConfig { 
    private SpringSwaggerConfig springSwaggerConfig; 
    @Autowired 
    public void setSpringSwaggerConfig(SpringSwaggerConfig springSwaggerConfig) { 
    this.springSwaggerConfig = springSwaggerConfig; 
    } 
    @Bean 
    public SwaggerSpringMvcPlugin customImplementation() { 
    return new SwaggerSpringMvcPlugin(this.springSwaggerConfig) 
    // Root level documentation 
    .apiInfo(new ApiInfo("Swagger-demo JSON API", "This service provides a JSON representation the service API", null, null, null, null)) 
    .useDefaultResponseMessages(false) 
    // Map the specific URL patterns into Swagger 
    .includePatterns("/greeting.*"); 
    } 
} 

आपका स्वैगर अब ऊपर और चलना चाहिए। /project-name/swagger/index.html तक पहुंचने का प्रयास करें।

1

मैं इसे एक ग्रेडल आधारित वसंत बूट एप्लिकेशन के अंदर 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 होगा आप इसे उपरोक्त उत्तर के रूप में भी अनुकूलित कर सकते हैं।

0

मुझे भी एक ही समस्या का सामना करना पड़ता है। मैं जेसन को देखने में सक्षम था लेकिन dest फ़ोल्डर को जोड़ने के बाद ui swagger नहीं। इनिशियलाइज़ वर्ग जो SpringBootServletInitializer मैं विधि नीचे जुड़ते फैली में तो यह एकदम सही काम कर रहा था

@Override 
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { 
    return application.sources(SampleWebStaticApplication.class); 
} 

उल्लेख link