【发布时间】: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