簡體   English   中英

Swagger for Spring MVC項目

[英]Swagger for Spring MVC project

關於在Spring MVC中集成Swagger:

Swagger沒有顯示@RequestMappingGET/PUT/POST文檔

在我的Spring MVC Rest webservice應用程序中,我有一個Login控制器和一個Student Controller。 我剛剛配置了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/>

無論現在看到什么輸出是好的,我們都不會在這個JSON輸出中看到swagger UI和GET/POST/PUT方法級別映射。 所以沒關系。 它僅顯示類級別路徑。

要查看具有GET/POST/PUT方法級別映射和URL的實際Swagger UI,我們需要下載可在此處獲得的SwaggerUIhttps//github.com/swagger-api/swagger-ui

然后導航到此index.html文件: swagger-ui-master\\swagger-ui-master\\dist\\index.html此處,編輯應用程序api-docs URL的源JSON URL:

即:

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

現在你看到了一切!

我只有一步之遙......

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM