我有一个自定义客户端工具,该工具向RESTful服务器API发出HTTP请求。
请求可能需要约1分钟的时间(文件是作为用户配置文件的一部分以多部分方式上传的),并且客户端进程可能会被用户终止或关闭终端窗口。结果所有文件都没有上传。
如果客户端工具被终止(终止连接),则在服务器日志中会显示java.io.IOException
,但看起来Exception
已由容器处理/消耗(使用WAS Liberty Profile)。发生这种情况时,我想在服务器端进行一些其他处理,以清理不完整的配置文件。
我正在尝试使用ExceptionMapper
来捕捉IOException
,但是从未调用toResponse()
。我是否缺少某些东西?
MyApplication.java
:
package com.test.server;
import com.test.exceptions.IOExceptionMapper;
import com.test.server.Service;
import java.util.HashSet;
import java.util.Set;
import javax.ws.rs.core.Application;
public class MyApplication extends Application {
@Override
public final Set<Class<?>> getClasses() {
Set<Class<?>> classes = new HashSet<Class<?>>();
classes.add(MyService.class);
classes.add(MyApplication.class);
classes.add(IOExceptionMapper.class);
return classes;
}
}
MyService.java
:
package com.test.server;
@WebService
@Path("/")
public class MyService {
@POST
@Path("/profile")
@Produces(MediaType.TEXT_PLAIN)
public Response createProfile(@javax.ws.rs.core.Context final HttpHeaders httpHeaders,
@javax.ws.rs.core.Context final HttpServletRequest request) throws IOException {
// do things, duration is ~1 minute
ResponseBuilder rb = Response.status(Status.OK).entity("Created.");
return rb.build();
}
}
IOExceptionMapper.java
:
package com.test.exceptions;
import java.io.IOException;
import javax.inject.Singleton;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;
@Provider
@Singleton
public class IOExceptionMapper implements ExceptionMapper<IOException> {
@Override
public Response toResponse(IOException ex) {
System.out.println("toResponse IOExceptionMapper");
return Response.status(404).entity(ex.getMessage()).type("text/plain").build();
}
}
来自web.xml
的摘要:
<servlet>
<description>API</description>
<display-name>RESTAPI</display-name>
<servlet-name>RESTAPI</servlet-name>
<servlet-class>com.ibm.websphere.jaxrs.server.IBMRestServlet</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>com.test.server.MyApplication</param-value>
</init-param>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.test.exceptions</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
服务器日志:
[INFO ] FFDC1015I: An FFDC Incident has been created: "java.io.IOException: Connection closed: Read failed. Possible end of stream encountered. com.ibm.ws.webcontainer.security.PostParameterHelper 402" at ffdc_19.01.30_18.23.41.0.log
[INFO ] FFDC1015I: An FFDC Incident has been created: "java.io.IOException: Connection closed: Read failed. Possible end of stream encountered. com.ibm.wsspi.http.ee7.HttpInputStreamEE7 129" at ffdc_19.01.30_18.23.41.1.log
[INFO ] FFDC1015I: An FFDC Incident has been created: "java.io.IOException: SRVE0216E: post body contains less bytes than specified by content-length. com.ibm.ws.webcontainer.security.PostParameterHelper 402" at ffdc_19.01.30_18.23.41.2.log
[INFO ] FFDC1015I: An FFDC Incident has been created: "java.io.IOException: Connection closed: Read failed. Possible end of stream encountered. org.apache.cxf.transport.http.AbstractHTTPDestination$BackChannelConduit 786" at ffdc_19.01.30_18.23.45.0.log
[INFO ] FFDC1015I: An FFDC Incident has been created: "java.io.IOException: Connection closed: Read failed. Possible end of stream encountered. com.ibm.ws.webcontainer.srt.SRTServletRequest.finish 875" at ffdc_19.01.30_18.23.45.1.log