简体   繁体   English

Swagger for Spring MVC项目

[英]Swagger for Spring MVC project

Regarding integrating Swagger in Spring MVC: 关于在Spring MVC中集成Swagger:

Swagger is not displaying the GET/PUT/POST documentation for @RequestMapping Swagger没有显示@RequestMappingGET/PUT/POST文档

In my Spring MVC Rest webservice application, I have a Login controller and a Student Controller. 在我的Spring MVC Rest webservice应用程序中,我有一个Login控制器和一个Student Controller。 I just configured Swagger to generate the Rest API documentation. 我刚刚配置了Swagger来生成Rest API文档。 Reference: http://java.dzone.com/articles/how-configure-swagger-generate 参考: http//java.dzone.com/articles/how-configure-swagger-generate

Question : However, Swagger is displaying only the class-level path, and I guess its not wven displaying the class level @RequestMapping . :然而,Swagger只显示类级路径,我猜它不是显示类级别@RequestMapping , The method level mappings are ignored. 方法级别映射被忽略。 Any reason why ? 有什么理由吗?

@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: 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;
    }

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

Update: 更新:

you also need the below config in the spring config XML file, as mentioned in https://github.com/martypitt/swagger-springmvc 你还需要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/>

Whatever output seeing now is good, we won't see the swagger UI and the GET/POST/PUT method level mappings here in this JSON output. 无论现在看到什么输出是好的,我们都不会在这个JSON输出中看到swagger UI和GET/POST/PUT方法级别映射。 So that's fine. 所以没关系。 It shows only the class level path. 它仅显示类级别路径。

To see the actual Swagger UI with the GET/POST/PUT method level mappings, and the URL's, we need to download the SwaggerUI which is available here: https://github.com/swagger-api/swagger-ui 要查看具有GET/POST/PUT方法级别映射和URL的实际Swagger UI,我们需要下载可在此处获得的SwaggerUIhttps//github.com/swagger-api/swagger-ui

And then navigate to this index.html file: swagger-ui-master\\swagger-ui-master\\dist\\index.html here, edit the source JSON URL to your application api-docs URL : 然后导航到此index.html文件: swagger-ui-master\\swagger-ui-master\\dist\\index.html此处,编辑应用程序api-docs URL的源JSON URL:

ie: 即:

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

Now you see everything!!! 现在你看到了一切!

I was just one step away... 我只有一步之遥......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM