Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e8dadff

Browse files
committed
Removes jsp from swagger-ui
- Adds an endpoint to list all of the swagger resources within a running application - Added a base class for swagger contract tests
1 parent cb0e084 commit e8dadff

18 files changed

Lines changed: 293 additions & 195 deletions

File tree

gradle/idea.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ idea {
2727
if (aslCopyright == null) {
2828
copyrightManager.append(new XmlParser().parseText("""
2929
<copyright>
30-
<option name="notice" value="&#10;&#10;Copyright \${today.year} the original author or authors.&#10;&#10;Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);&#10;you may not use this file except in compliance with the License.&#10;You may obtain a copy of the License at&#10;&#10; http://www.apache.org/licenses/LICENSE-2.0&#10;&#10;Unless required by applicable law or agreed to in writing, software&#10;distributed under the License is distributed on an &quot;AS IS&quot; BASIS,&#10;WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.&#10;See the License for the specific language governing permissions and&#10;limitations under the License.&#10;&#10;" />
30+
<option name="notice" value="&#10;&#10;&#10;&#10;Copyright \${today.year} the original author or authors.&#10;&#10;Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);&#10;you may not use this file except in compliance with the License.&#10;You may obtain a copy of the License at&#10;&#10; http://www.apache.org/licenses/LICENSE-2.0&#10;&#10;Unless required by applicable law or agreed to in writing, software&#10;distributed under the License is distributed on an &quot;AS IS&quot; BASIS,&#10;WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.&#10;See the License for the specific language governing permissions and&#10;limitations under the License.&#10;&#10;" />
3131
<option name="keyword" value="Copyright" />
3232
<option name="allowReplaceKeyword" value="" />
3333
<option name="myName" value="ASL2" />

springfox-spring-web/src/main/java/springfox/documentation/spring/web/DocumentationCache.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
import springfox.documentation.service.Documentation;
2323

24-
import java.util.Collection;
24+
import java.util.Collections;
2525
import java.util.Map;
2626

2727
import static com.google.common.collect.Maps.*;
@@ -37,7 +37,7 @@ public Documentation documentationByGroup(String groupName) {
3737
return documentationLookup.get(groupName);
3838
}
3939

40-
public Collection<Documentation> all() {
41-
return documentationLookup.values();
40+
public Map<String, Documentation> all() {
41+
return Collections.unmodifiableMap(documentationLookup);
4242
}
4343
}

springfox-swagger-ui/build.gradle

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,20 +58,15 @@ task unzip(type: Copy) {
5858
into file("${buildDir}")
5959
}
6060

61-
/**
62-
* Creates the jsp from index.html
63-
*/
6461
task sdoc(type: Copy) {
65-
from("${projectDir}/src/jsps/sdoc.jsp")
66-
from("${projectDir}/src/jsps/swaggerResources.jsp")
62+
from("${projectDir}/src/web/swagger-ui.html")
6763
into("${buildDir}/${swaggerUiExplodedDir}")
6864
}
6965

7066
task removeHtmlIndex(type: Delete) {
7167
delete "${buildDir}/${swaggerUiExplodedDir}/index.html"
7268
}
7369

74-
7570
jar {
7671
from("${buildDir}/${swaggerUiExplodedDir}") {
7772
into "META-INF/resources/webjars/${project.name}/${project.version}"

springfox-swagger-ui/src/jsps/swaggerResources.jsp

Lines changed: 0 additions & 18 deletions
This file was deleted.
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
*
3+
* Copyright 2015 the original author or authors.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*
18+
*/
19+
package springfox.documentation.swagger.ui;
20+
21+
import com.google.common.base.Optional;
22+
import org.springframework.beans.factory.annotation.Autowired;
23+
import org.springframework.beans.factory.annotation.Value;
24+
import org.springframework.http.HttpStatus;
25+
import org.springframework.http.ResponseEntity;
26+
import org.springframework.stereotype.Controller;
27+
import org.springframework.web.bind.annotation.RequestMapping;
28+
import org.springframework.web.bind.annotation.ResponseBody;
29+
import springfox.documentation.annotations.ApiIgnore;
30+
import springfox.documentation.service.Documentation;
31+
import springfox.documentation.spring.web.DocumentationCache;
32+
33+
import java.util.ArrayList;
34+
import java.util.Collections;
35+
import java.util.List;
36+
import java.util.Map;
37+
38+
@Controller
39+
@ApiIgnore
40+
public class ApiResourceController {
41+
42+
@Value("${springfox.documentation.swagger.v1.path:/api-docs}")
43+
private String swagger1Url;
44+
45+
@Value("${springfox.documentation.swagger.v2.path:/v2/api-docs}")
46+
private String swagger2Url;
47+
@Autowired
48+
private DocumentationCache documentationCache;
49+
50+
51+
@RequestMapping(value = "/swagger-resources")
52+
@ResponseBody
53+
ResponseEntity<List<SwaggerResource>> swaggerResources() {
54+
Class swagger1Controller = classOrNull("springfox.documentation.swagger.web.DefaultSwaggerController");
55+
Class swagger2Controller = classOrNull("springfox.documentation.swagger2.web.Swagger2Controller");
56+
57+
List<SwaggerResource> resources = new ArrayList<SwaggerResource>();
58+
59+
for (Map.Entry<String, Documentation> entry : documentationCache.all().entrySet()) {
60+
String swaggerGroup = entry.getKey();
61+
if (swagger1Controller != null) {
62+
SwaggerResource swaggerResource = resource(swaggerGroup, swagger1Url);
63+
swaggerResource.setSwaggerVersion("1.2");
64+
resources.add(swaggerResource);
65+
}
66+
67+
if (swagger2Controller != null) {
68+
SwaggerResource swaggerResource = resource(swaggerGroup, swagger2Url);
69+
swaggerResource.setSwaggerVersion("2.0");
70+
resources.add(swaggerResource);
71+
}
72+
}
73+
Collections.sort(resources);
74+
return new ResponseEntity<List<SwaggerResource>>(resources, HttpStatus.OK);
75+
}
76+
77+
private SwaggerResource resource(String swaggerGroup, String baseUrl) {
78+
SwaggerResource swaggerResource = new SwaggerResource();
79+
swaggerResource.setName(swaggerGroup);
80+
swaggerResource.setLocation(swaggerLocation(baseUrl, swaggerGroup));
81+
return swaggerResource;
82+
}
83+
84+
private String swaggerLocation(String swaggerUrl, String swaggerGroup) {
85+
String base = Optional.of(swaggerUrl).get();
86+
return base + "?group=" + swaggerGroup;
87+
}
88+
89+
private Class classOrNull(String className) {
90+
try {
91+
return Class.forName(className);
92+
} catch (ClassNotFoundException e) {
93+
return null;
94+
}
95+
}
96+
}

springfox-swagger-ui/src/main/java/springfox/documentation/swagger/ui/ApiResourceLocator.java

Lines changed: 0 additions & 49 deletions
This file was deleted.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
*
3+
* Copyright 2015 the original author or authors.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*
18+
*/
19+
20+
package springfox.documentation.swagger.ui;
21+
22+
import org.springframework.context.annotation.Import;
23+
24+
import java.lang.annotation.Documented;
25+
import java.lang.annotation.Retention;
26+
import java.lang.annotation.Target;
27+
28+
@Retention(value = java.lang.annotation.RetentionPolicy.RUNTIME)
29+
@Target(value = {java.lang.annotation.ElementType.TYPE})
30+
@Documented
31+
@Import(SwaggerUiConfig.class)
32+
public @interface EnableSwaggerUi {
33+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
*
3+
* Copyright 2015 the original author or authors.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*
18+
*/
19+
20+
package springfox.documentation.swagger.ui;
21+
22+
import com.google.common.collect.ComparisonChain;
23+
24+
public class SwaggerResource implements Comparable<SwaggerResource> {
25+
private String name;
26+
private String location;
27+
private String swaggerVersion;
28+
29+
public String getName() {
30+
return name;
31+
}
32+
33+
public void setName(String name) {
34+
this.name = name;
35+
}
36+
37+
public String getLocation() {
38+
return location;
39+
}
40+
41+
public void setLocation(String location) {
42+
this.location = location;
43+
}
44+
45+
public String getSwaggerVersion() {
46+
return swaggerVersion;
47+
}
48+
49+
public void setSwaggerVersion(String swaggerVersion) {
50+
this.swaggerVersion = swaggerVersion;
51+
}
52+
53+
@Override
54+
public int compareTo(SwaggerResource other) {
55+
return ComparisonChain.start()
56+
.compare(this.swaggerVersion, other.swaggerVersion)
57+
.compare(this.name, other.name)
58+
.result();
59+
}
60+
}

springfox-swagger-ui/src/main/java/springfox/documentation/swagger/ui/SwaggerApi.java renamed to springfox-swagger-ui/src/main/java/springfox/documentation/swagger/ui/SwaggerUiConfig.java

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,13 @@
1616
*
1717
*
1818
*/
19-
package springfox.documentation.swagger.ui;
20-
21-
public class SwaggerApi {
22-
private String uri;
23-
private String title;
2419

25-
public String getUri() {
26-
return uri;
27-
}
28-
29-
public void setUri(String uri) {
30-
this.uri = uri;
31-
}
20+
package springfox.documentation.swagger.ui;
3221

33-
public String getTitle() {
34-
return title;
35-
}
22+
import org.springframework.context.annotation.ComponentScan;
23+
import org.springframework.context.annotation.Configuration;
3624

37-
public void setTitle(String title) {
38-
this.title = title;
39-
}
25+
@Configuration
26+
@ComponentScan(basePackageClasses = {ApiResourceController.class})
27+
public class SwaggerUiConfig {
4028
}

springfox-swagger-ui/src/test/groovy/springfox/documentation/swagger/ui/ApiResourceLocatorTest.groovy

Lines changed: 0 additions & 41 deletions
This file was deleted.

0 commit comments

Comments
 (0)