Websphere 9中使用Swagger出现javax.ws.rs.ClientErrorException:HTTP 404错误
运行在WebSphere 9.0上的JAX-RS应用集成了Swagger,目前遇到HTTP 404错误,栈跟踪信息显示底层存在NumberFormatException,具体是尝试将字符串"null"转换为Integer时失败。
栈跟踪信息
javax.ws.rs.ClientErrorException: HTTP 404 Not Found at
org.apache.cxf.jaxrs.utils.SpecExceptions.toHttpException(SpecExceptions.java:117)
12 lines skipped for [org.apache.cxf] at
com.ibm.ws.jaxrs20.endpoint.AbstractJaxRsWebEndpoint.invoke(AbstractJaxRsWebEndpoint.java:147)
at
com.ibm.websphere.jaxrs.server.IBMRestServlet.handleRequest(IBMRestServlet.java:192)
at
com.ibm.websphere.jaxrs.server.IBMRestServlet.doGet(IBMRestServlet.java:158)
1 line skipped for [javax.servlet] at
com.ibm.websphere.jaxrs.server.IBMRestServlet.service(IBMRestServlet.java:138)
at
com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java:1235)
at
com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:779)
at
com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:478)
at
com.ibm.ws.webcontainer.servlet.ServletWrapperImpl.handleRequest(ServletWrapperImpl.java:179)
at
com.ibm.ws.webcontainer.filter.WebAppFilterManager.invokeFilters(WebAppFilterManager.java:1134)
at
com.ibm.ws.webcontainer.servlet.CacheServletWrapper.handleRequest(CacheServletWrapper.java:82)
at
com.ibm.ws.webcontainer.WebContainer.handleRequest(WebContainer.java:966)
at
com.ibm.ws.webcontainer.WSWebContainer.handleRequest(WSWebContainer.java:1817)
at
com.ibm.ws.webcontainer.channel.WCChannelLink.ready(WCChannelLink.java:382)
at
com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleDiscrimination(HttpInboundLink.java:465)
at
com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleNewRequest(HttpInboundLink.java:532)
at
com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.processRequest(HttpInboundLink.java:318)
at
com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.ready(HttpInboundLink.java:289)
at
com.ibm.ws.ssl.channel.impl.SSLConnectionLink.determineNextChannel(SSLConnectionLink.java:1187)
at
com.ibm.ws.ssl.channel.impl.SSLConnectionLink$MyReadCompletedCallback.complete(SSLConnectionLink.java:694)
at
com.ibm.ws.ssl.channel.impl.SSLReadServiceContext$SSLReadCompletedCallback.complete(SSLReadServiceContext.java:1833)
at
com.ibm.ws.tcp.channel.impl.AioReadCompletionListener.futureCompleted(AioReadCompletionListener.java:175)
at
com.ibm.io.async.AbstractAsyncFuture.invokeCallback(AbstractAsyncFuture.java:217)
at
com.ibm.io.async.AsyncChannelFuture.fireCompletionActions(AsyncChannelFuture.java:161)
at com.ibm.io.async.AsyncFuture.completed(AsyncFuture.java:138) at
com.ibm.io.async.ResultHandler.complete(ResultHandler.java:204) at
com.ibm.io.async.ResultHandler.runEventProcessingLoop(ResultHandler.java:775)
at com.ibm.io.async.ResultHandler$2.run(ResultHandler.java:905) at
com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:1909)
Caused by:
java.lang.NumberFormatException: For input string: "null" at
java.lang.NumberFormatException.forInputString(NumberFormatException.java:76)
at java.lang.Integer.parseInt(Integer.java:592) at
java.lang.Integer.(Integer.java:879) 3 lines skipped for
[sun.] at
java.lang.reflect.Constructor.newInstance(Constructor.java:437) 1
line skipped for [org.apache.cxf]
pom.xml内容
<dependencies> <dependency> <groupId>io.swagger</groupId> <artifactId>swagger-annotations</artifactId> <version>1.6.1</version> <scope>provided</scope> </dependency> </dependencies> <profiles> <profile> <id>gerar-swagger</id> <build> <plugins> <plugin> <groupId>com.github.kongchen</groupId> <artifactId>swagger-maven-plugin</artifactId> <version>3.1.8</version> <configuration> <apiSources> <apiSource> <springmvc>false</springmvc> <locations> <location>br.com.api</location> </locations> <schemes> <schemes>http</schemes> <schemes>https</schemes> </schemes> <host>hostName</host> <basePath>/dir/v1</basePath> <outputFormats>yaml</outputFormats> <swaggerDirectory>${basedir}</swaggerDirectory> <info> <title>API</title> <version>1.2.3.23</version> <description> API </description> </info> </apiSource> </apiSources> </configuration> <executions> <execution> <phase>compile</phase> <goals> <goal>generate</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </profile> </profiles>
解决方案
根因分析
底层的NumberFormatException是核心问题:某个Integer类型的接口参数收到了字符串"null",WebSphere的CXF解析器尝试将其转换为Integer时失败,导致无法匹配到对应的资源方法,最终返回HTTP 404错误。
具体修复步骤
排查接口参数定义
遍历所有JAX-RS接口,找出使用@PathParam、@QueryParam等注解标注的Integer/int类型参数,检查是否存在请求传入"null"字符串的场景(比如URL路径参数写为/users/null,或查询参数?id=null)。
注意:如果用int基本类型接收参数,当传入null字符串时会直接触发转换异常,建议改用Integer包装类,并添加空值校验。修复参数校验逻辑
对非必填参数,使用Integer包装类而非int基本类型,在方法内部先判断参数是否为null或无效值;对必填参数,添加@NotNull、@Pattern(regexp = "\\d+")等校验注解,确保传入的是有效数字字符串。检查Swagger文档生成配置
查看swagger-maven-plugin生成的yaml文档,确认所有Integer类型参数的定义是否正确,避免出现参数默认值设为"null"、参数类型标记错误等问题,防止前端根据错误文档发起无效请求。验证请求路径匹配
确认请求URL包含pom.xml中配置的basePath(即/dir/v1),且路径与接口的@Path注解完全匹配,避免因路径不匹配导致的404。WebSphere配置调整(可选)
如果需要临时放宽参数解析规则,可在WebSphere控制台配置CXF的jaxrs.validation.disable属性为true,但不推荐长期使用,建议优先从代码层面修复参数问题。
内容的提问来源于stack exchange,提问作者Luciana Oliveira

