java - Swagger 的 Spring MVC 项目

标签 java spring-mvc swagger

关于在 Spring MVC 中集成 Swagger:

Swagger 未显示 @RequestMappingGET/PUT/POST 文档

在我的 Spring MVC Rest web 服务应用程序中,我有一个登录 Controller 和一个学生 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 配置 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,我们需要 下载此处提供的 SwaggerUI:https://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'],

现在你看到了一切!!!

我只差一步......

关于java - Swagger 的 Spring MVC 项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26807791/

相关文章:

java - Parse 教程的问题 (Android)

java - Spring MVC View 和更新

attributes - 通过属性版本控制在 Swagger 中利用 MultipleApiVersions

yaml - 如何在 Swagger API 中描述嵌套对象?

arrays - 使用 OpenAPI v3 规范定义一个或多个字符串数组的正确方法

c# - 跨平台、稳定、功能强大的网络平台

java - 如何使用@RequestBody 调用@RestController?

java - 如何将大整数转换为二进制?

JavaScript 文件未加载

java - 如何在 Spring MVC 和 Hibernate 应用程序中使用分页