我是Spring MVC
和Swagger
版本0.8.8
实施的新手。我关注此链接 https://dzone.com/articles/how-configure-swagger-generate 并尝试在我的应用程序中实现它。出于这个原因,我做了以下更改:
@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("Test API", "API for Testing",
"test.html", "support@support.com",
"License Type", "http://www.test.com");
}
}
我在pom.xml中添加了以下内容
<repositories>
<repository>
<id>jcenter-release</id>
<name>jcenter</name>
<url>http://oss.jfrog.org/artifactory/oss-release-local/</url>
</repository>
</repositories>
依赖:
<dependency>
<groupId>com.mangofactory</groupId>
<artifactId>swagger-springmvc</artifactId>
<version>0.9.4</version>
</dependency>
我在下面补充说:
<dependency>
<groupId>org.ajar</groupId>
<artifactId>swagger-spring-mvc-ui</artifactId>
<version>0.1</version>
<scope>compile</scope>
</dependency>
在我的applicationContext.xml中,我添加了以下
<bean id="swaggerConfig" class="com.commons.test.SwaggerConfig"/>
<bean class="com.mangofactory.swagger.configuration.SpringSwaggerConfig" />
<mvc:resources mapping="/docs/**" location="/docs/"/>
<mvc:default-servlet-handler/>
我也将Swagger UI
复制到我的项目中。
Swagger UI
是Swagger's JSON
的好看的JavaScript客户端。我使用以下步骤对其进行了集成:
git clone https://github.com/wordnik/swagger-ui
cp -r swagger-ui/dist ~/test-rest/src/main/webapp/docs
但是,当我启动应用网址时: http://localhost:8080/test-rest/docs/index.html
无法从http://localhost:8080/test-rest/api-docs/index.html
中读取招摇的JSON在我的index.html中,我正在使用以下内容,请告诉我出了什么问题?
<script type="text/javascript">
$(function () {
var docUrl = window.location.href;
var apiDocUrl = docUrl.replace("/docs", "/api-docs");
window.swaggerUi = new SwaggerUi({
url: apiDocUrl,
dom_id: "swagger-ui-container",
supportedSubmitMethods: ['get', 'post', 'put', 'delete'],
onComplete: function(swaggerApi, swaggerUi){
log("Loaded SwaggerUI");
if(typeof initOAuth == "function") { }
$('pre code').each(function(i, e) {
hljs.highlightBlock(e)
});
},
onFailure: function(data) {
log("Unable to Load SwaggerUI");
},
docExpansion: "none",
sorter : "alpha"
});
$('#input_apiKey').change(function() {
var key = $('#input_apiKey')[0].value;
log("key: " + key);
if(key && key.trim() != "") {
log("added key " + key);
window.authorizations.add("key", new ApiKeyAuthorization("api_key", key, "query"));
}
})
window.swaggerUi.load();
});
</script>
</head>
CURL命令给出了原始json的输出,但ui部分根本没有渲染。