我是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"}}
Ny
@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,这里缺少什么?我需要更改/添加/修改我的代码?
答案 0 :(得分:0)
根据文章,您将看到一个部分,告诉您在哪里可以找到文档。我不确定你是否使用弹簧靴但是......
进行这些更改后,我可以使用“mvn spring-boot:run”打开应用程序并在浏览器中查看http://localhost:8080/docs/index.html。
无论如何,swagger-springmvc现在被称为springfox,并支持最新的swagger规范(2.0)。还有documentation available可帮助您入门。我建议使用springfox的latest version(撰写本文时为2.3.1)。