Swagger用于Spring MVC项目


问题内容

关于将Swagger集成到Spring MVC中:

Swagger没有显示有关的GET/PUT/POST文档@RequestMapping

在我的Spring MVC Rest Web服务应用程序中,我有一个Login控制器和一个Student控制器。我刚刚配置了Swagger来生成Rest
API文档。参考: http
//java.dzone.com/articles/how-configure-swagger-
generate

问题 :但是,Swagger仅显示类级别的路径,我想它不显示类级别的路径@RequestMapping。, 方法级别映射将被忽略。
有什么原因
吗?

@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配置:

@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", "", "contact@localhost.com", "License type",
                "something like a License URL");
        return apiInfo;
    }

输出:

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: "abc@xyz.com",
license: "sometext",
licenseUrl: "License URL"
}
}

更新:

您还需要spring config XML文件中的以下配置,如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/>

问题答案:

无论现在看到什么输出都是好的,GET/POST/PUT在此JSON输出中,我们都不会在此处看到大方的UI和方法级别的映射。这样很好
它仅显示类级别的路径。

要查看带有GET/POST/PUT方法级别映射和URL的实际Swagger UI ,我们需要下载Swagger
UI,可从以下位置下载SwaggerUIhttps :
//github.com/swagger-api/swagger-ui

然后导航到该index.html文件:swagger-ui-master\swagger-ui-master\dist\index.html
在这里,将源JSON URL编辑为您的应用程序api-docs URL:

即:

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

现在您看到了一切!!!

我只有一步之遥…