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/>
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
Trạng thái HTTP 404 – spiderman
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