【问题标题】:Swagger not giving UI for Spring MVC ProjectSwagger 没有为 Spring MVC 项目提供 UI
【发布时间】:2016-04-13 15:55:52
【问题描述】:

我是 Swagger 的新手,并试图在 Spring MVC 中实现它。我正在使用来自http://mvnrepository.com/artifact/com.mangofactory/swagger-springmvc 的最新依赖项swagger-springmvc。所以基于链接https://dzone.com/articles/documenting-your-spring-api。我在mvc-config.xml中添加了以下配置。

<!-- Serve static content - required for Swagger -->
    <mvc:default-servlet-handler/>

    <!-- 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"/>

我也使用了上面显示的链接。

<bean class="com.xxx.xx.xx.SwaggerConfig"/>

然后我添加了

git clone https://github.com/wordnik/swagger-ui
cp -r swagger-ui/dist ~/dev/x-auth-security/src/main/webapps/docs

当我启动网站时:http://localhost:8080/dp-rest/api-docs 我没有看到 UI 格式,它只提供 JSON 格式。

{"apiVersion":"1.0","swaggerVersion":"1.2","apis":[{"path":"/default/student-service","description":"Manage Student Service","position":0},{"path":"/default/student-service","description":"Manage Student Service","position":0}],"authorizations":[],"info":{"title":"Student API's","description":"API for Student ","termsOfServiceUrl":"terms.html","contact":"test@yahoo.com","license":"Commercial Proprietary","licenseUrl":"http://www.adbc.com"}}

纽约

@Configuration
@EnableSwagger
public class SwaggerConfig {
    private SpringSwaggerConfig springSwaggerConfig;

    @Autowired
    public void setSpringSwaggerConfig(SpringSwaggerConfig springSwaggerConfig) {
        this.springSwaggerConfig = springSwaggerConfig;
    }

    @Bean
    // Don't forget the @Bean annotation
    public SwaggerSpringMvcPlugin customImplementation() {
        return new SwaggerSpringMvcPlugin(this.springSwaggerConfig).apiInfo(
                apiInfo()).includePatterns(".*");
    }

    private ApiInfo apiInfo() {
        return new ApiInfo("Student API", "API for Student",
                "term.html", "test@tahoo.com",
                "Commercial Proprietary", "http://www.test.com");
    }
}

为什么当我们启动http://localhost:8080/sample-rest/api-docs 网站时没有出现 UI 格式? 然后只有我看到原始 JSON 响应没有任何 ui,这里缺少什么?我需要更改/添加/修改我的代码吗?

【问题讨论】:

    标签: spring-mvc swagger swagger-ui


    【解决方案1】:

    根据文章,您将看到一个部分,告诉您在哪里可以找到文档。我不确定您是否使用的是 spring-boot 但是...

    进行这些更改后,我可以使用“mvn spring-boot:run”打开应用程序并在浏览器中查看http://localhost:8080/docs/index.html

    无论如何,swagger-springmvc 现在被称为springfox 并且支持最新的swagger 规范(2.0)。还有documentation available 可以帮助您入门。我建议改用 springfox 的latest version(撰写本文时为 2.3.1)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-18
      • 2020-11-11
      • 1970-01-01
      • 1970-01-01
      • 2016-12-14
      • 1970-01-01
      • 2015-10-31
      • 2016-03-01
      相关资源
      最近更新 更多