2014-11-07 21 views
6

Về tích hợp Swagger trong Spring MVC:Swagger cho dự án Spring MVC

Swagger không hiển thị các tài liệu GET/PUT/POST cho @RequestMapping

Trong ứng dụng webservice Spring MVC Nghỉ ngơi của tôi, tôi có một bộ điều khiển Đăng nhập và điều khiển cho sinh viên. Tôi vừa định cấu hình Swagger để tạo tài liệu Rest API. tham khảo: http://java.dzone.com/articles/how-configure-swagger-generate

Câu hỏi: Tuy nhiên, Swagger đang hiển thị chỉ đường dẫn lớp cấp, và tôi đoán nó không wven hiển thị mức độ lớp @RequestMapping. , Ánh xạ mức phương thức bị bỏ qua. Bất kỳ lý do gì tại sao?

@Controller 
@RequestMapping(value = "/login") 
public class LoginController { 


@RestController 
@RequestMapping(value = "/students/") 
public class StudentController { 

    @RequestMapping(value = "{departmentID}", method = RequestMethod.GET) 
    public MyResult getStudents(@PathVariable String departmentID) { 
     // code 
    } 

    @RequestMapping(value = "student", method = RequestMethod.GET) 
    public MyResult getStudentInfo(
     @RequestParam(value = "studentID") String studentID, 
     @RequestParam(value = "studentName") String studentName) { 
    //code 
    } 

    @RequestMapping(value = "student", method = RequestMethod.POST) 
    public ResponseEntity<Student> updateStudentInfo(@RequestBody Student student) { 
     //code 
    } 

Swagger Cấu hình:

@Configuration 
@EnableSwagger 
public class SwaggerConfiguration { 
    private SpringSwaggerConfig swaggerConfig; 

    @Autowired 
    public void setSpringSwaggerConfig(SpringSwaggerConfig swaggerConfig) { 
     this.swaggerConfig = swaggerConfig; 
    } 

    @Bean 
    // Don't forget the @Bean annotation 
    public SwaggerSpringMvcPlugin customImplementation() { 
     return new SwaggerSpringMvcPlugin(this.swaggerConfig).apiInfo(
       apiInfo()).includePatterns("/.*"); 
    } 

private ApiInfo apiInfo() { 
     ApiInfo apiInfo = new ApiInfo("my API", "API for my app", "", "[email protected]", "License type", 
       "something like a License URL"); 
     return apiInfo; 
    } 

Output:

http://localhost:8080/studentapplication/api-docs 

{ 
apiVersion: "1.0", 
swaggerVersion: "1.2", 
apis: [ 
{ 
path: "/default/login-controller", 
description: "Login Controller" 
}, 
{ 
path: "/default/student-controller", 
description: "Student Controller" 
} 
], 
info: { 
title: "Student API", 
description: "API for Student", 
termsOfServiceUrl: "StudentApp API terms of service", 
contact: "[email protected]", 
license: "sometext", 
licenseUrl: "License URL" 
} 
} 

Cập nhật:

bạn cũng cần cấu hình dưới đây trong file XML mùa xuân cấu hình, như đã đề cập trong https://github.com/martypitt/swagger-springmvc

<!-- to enable the default documentation controller--> 
    <context:component-scan base-package="com.mangofactory.swagger.controllers"/> 

    <!-- to pick up the bundled spring configuration--> 
    <context:component-scan base-package="com.mangofactory.swagger.configuration"/> 

    <!-- Direct static mappings --> 
    <mvc:resources mapping="*.html" location="/, classpath:/swagger-ui"/> 

    <!-- Serve static content--> 
    <mvc:default-servlet-handler/> 
+0

Kiểm tra đầu ra của http: // localhost: 8080/studentapplication/api-docs/default/login-điều khiển và http: // localhost: 8080/studentapplication/api-docs // default/student-controller – Ron

+0

Trạng thái HTTP 404 – spiderman

+1

OK. Tôi không biết các nhà phát triển của swagger-springmvc trông như thế nào trong SO, vì vậy nếu bạn không nhận được trả lời kịp thời, tôi khuyên bạn nên mở vấn đề trên kho lưu trữ của họ. – Ron

Trả lời

2

Bất kỳ đầu ra nào nhìn thấy bây giờ đều tốt, chúng tôi sẽ không thấy giao diện người dùng vênh và ánh xạ mức phương thức GET/POST/PUT ở đây trong đầu ra JSON này. Vì vậy, đó là tốt. Nó chỉ hiển thị đường dẫn cấp lớp.

Để xem Swagger UI thực tế với GET/POST/PUT ánh xạ mức độ phương pháp, và của URL, chúng ta cần để tải SwaggerUI trong đó có sẵn ở đây: https://github.com/swagger-api/swagger-ui

Và sau đó điều hướng đến index.html tập tin này: swagger-ui-master\swagger-ui-master\dist\index.html đây, chỉnh sửa mã nguồn JSON URL vào URL api-docs ứng dụng của bạn:

ví dụ:

$(function() { 
     window.swaggerUi = new SwaggerUi({ 
     url: "studentapplication/api-docs", 
     dom_id: "swagger-ui-container", 
     supportedSubmitMethods: ['get', 'post', 'put', 'delete'], 

Bây giờ bạn thấy tất cả mọi thứ !!!

Tôi chỉ là một bước đi ...

Các vấn đề liên quan