Skip to content

Commit d82edc0

Browse files
springbootdemo-tomcat11: port OpenApiServlet, add axis2.version property, drop dead OpenApiController
Three related fixes to make OpenAPI endpoints work on Tomcat 11: - Add OpenApiServlet.java (ported from springbootdemo WildFly module): HttpServlet delegating to SwaggerUIHandler for /openapi.json, /openapi.yaml, /swagger-ui — bypasses DispatcherServlet which is not wired in this WAR - Register OpenApiServlet in Axis2WebAppInitializer.addOpenApiServlet() so Tomcat routes GET /openapi.json to the correct handler instead of falling through to HTTPPostOnlyRejectionFilter - Delete dead OpenApiController.java: @GetMapping handlers are never invoked because DispatcherServlet is not configured in this WAR deployment - Add <axis2.version>2.0.1-SNAPSHOT</axis2.version> property and replace two hardcoded version strings in antrun .mar copy tasks with ${axis2.version} Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 4214047 commit d82edc0

4 files changed

Lines changed: 102 additions & 102 deletions

File tree

modules/samples/userguide/src/userguide/springbootdemo-tomcat11/pom.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
4343
<java.version>17</java.version>
4444
<spring-boot.version>3.4.3</spring-boot.version>
45+
<axis2.version>2.0.1-SNAPSHOT</axis2.version>
4546
</properties>
4647

4748
<dependencies>
@@ -370,6 +371,10 @@
370371
<include name="axis2.xml"/>
371372
</fileset>
372373
</copy>
374+
<mkdir dir="${project.build.directory}/deploy/axis2-json-api.war/WEB-INF/modules"/>
375+
<copy file="${settings.localRepository}/org/apache/axis2/axis2-openapi/${axis2.version}/axis2-openapi-${axis2.version}.jar"
376+
tofile="${project.build.directory}/deploy/axis2-json-api.war/WEB-INF/modules/openapi-${axis2.version}.mar"
377+
overwrite="true"/>
373378
<unzip src="${project.build.directory}/axis2-json-api-0.0.1-SNAPSHOT.war" dest="${project.build.directory}/exploded"/>
374379
<jar jarfile="${project.build.directory}/exploded/WEB-INF/services/Login.aar">
375380
<metainf file="resources-axis2/login_resources/services.xml"/>
@@ -385,6 +390,10 @@
385390
<include name="axis2.xml"/>
386391
</fileset>
387392
</copy>
393+
<mkdir dir="${project.build.directory}/exploded/WEB-INF/modules"/>
394+
<copy file="${settings.localRepository}/org/apache/axis2/axis2-openapi/${axis2.version}/axis2-openapi-${axis2.version}.jar"
395+
tofile="${project.build.directory}/exploded/WEB-INF/modules/openapi-${axis2.version}.mar"
396+
overwrite="true"/>
388397
</target>
389398
</configuration>
390399
<goals>

modules/samples/userguide/src/userguide/springbootdemo-tomcat11/src/main/java/userguide/springboot/configuration/Axis2WebAppInitializer.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,20 @@ public void onStartup(ServletContext container) {
5151
// Create the 'root' Spring application context
5252
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
5353

54-
addAxis2Servlet(container, ctx);
54+
addAxis2Servlet(container, ctx);
55+
addOpenApiServlet(container);
5556
logger.warn("onStartup() completed ...");
5657
}
57-
58-
private void addAxis2Servlet(ServletContext container, AnnotationConfigWebApplicationContext ctx) {
58+
59+
private void addOpenApiServlet(ServletContext container) {
60+
ServletRegistration.Dynamic openApi = container.addServlet(
61+
"OpenApiServlet", new OpenApiServlet());
62+
openApi.setLoadOnStartup(2);
63+
openApi.addMapping("/openapi.json", "/openapi.yaml", "/swagger-ui");
64+
logger.warn("OpenApiServlet registered at /openapi.json, /openapi.yaml, /swagger-ui");
65+
}
66+
67+
private void addAxis2Servlet(ServletContext container, AnnotationConfigWebApplicationContext ctx) {
5968

6069
ServletRegistration.Dynamic dispatcher = container.addServlet(
6170
"AxisServlet", new AxisServlet());

modules/samples/userguide/src/userguide/springbootdemo-tomcat11/src/main/java/userguide/springboot/configuration/OpenApiController.java

Lines changed: 0 additions & 99 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package userguide.springboot.configuration;
20+
21+
import jakarta.servlet.http.HttpServlet;
22+
import jakarta.servlet.http.HttpServletRequest;
23+
import jakarta.servlet.http.HttpServletResponse;
24+
import org.apache.axis2.context.ConfigurationContext;
25+
import org.apache.axis2.openapi.OpenApiModule;
26+
import org.apache.axis2.openapi.SwaggerUIHandler;
27+
import org.apache.axis2.transport.http.AxisServlet;
28+
import org.apache.commons.logging.Log;
29+
import org.apache.commons.logging.LogFactory;
30+
31+
import java.io.IOException;
32+
33+
/**
34+
* Servlet that serves OpenAPI documentation endpoints by delegating to
35+
* the Axis2 OpenAPI module's SwaggerUIHandler.
36+
*
37+
* Registered directly in Axis2WebAppInitializer at:
38+
* /openapi.json - OpenAPI 3.0.1 specification (JSON)
39+
* /openapi.yaml - OpenAPI 3.0.1 specification (YAML)
40+
* /swagger-ui - Interactive Swagger UI documentation
41+
*/
42+
public class OpenApiServlet extends HttpServlet {
43+
44+
private static final Log log = LogFactory.getLog(OpenApiServlet.class);
45+
46+
@Override
47+
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
48+
String uri = request.getRequestURI();
49+
log.info("OpenApiServlet.doGet() called for URI: " + uri);
50+
51+
ConfigurationContext configContext = (ConfigurationContext)
52+
getServletContext().getAttribute(AxisServlet.CONFIGURATION_CONTEXT);
53+
if (configContext == null) {
54+
log.warn("AxisServlet ConfigurationContext not found in ServletContext");
55+
response.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE, "OpenAPI module not available");
56+
return;
57+
}
58+
59+
SwaggerUIHandler handler = OpenApiModule.getSwaggerUIHandler(configContext);
60+
if (handler == null) {
61+
log.warn("OpenApiServlet: SwaggerUIHandler not found — ensure openapi module is in WEB-INF/modules");
62+
response.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE, "OpenAPI module not initialized");
63+
return;
64+
}
65+
66+
try {
67+
if (uri.endsWith("/openapi.json")) {
68+
handler.handleOpenApiJsonRequest(request, response);
69+
} else if (uri.endsWith("/openapi.yaml")) {
70+
handler.handleOpenApiYamlRequest(request, response);
71+
} else if (uri.contains("/swagger-ui")) {
72+
handler.handleSwaggerUIRequest(request, response);
73+
} else {
74+
response.sendError(HttpServletResponse.SC_NOT_FOUND);
75+
}
76+
} catch (Exception e) {
77+
log.error("OpenApiServlet error handling " + uri + ": " + e.getMessage(), e);
78+
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage());
79+
}
80+
}
81+
}

0 commit comments

Comments
 (0)