gpt4 book ai didi

java - org.glassfish.jersey.server.ContainerException : java. io.IOException:流关闭

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:39:42 29 4
gpt4 key购买 nike

我在扩展 JAX-RS 应用程序的应用程序类中注册了两个 ContainerRequestFilter 过滤器和一个 ContainerResponseFilter:

过滤为:

register(LoggingResponseFilter.class);
register(AuthorizationRequestFilter.class);
register(LikeRequestFilter.class);

当我尝试调用目标方法资源时出现异常。

资源方法为:

@Override
@POST
@Path("/{userId}/like")
@TokenResource
@GMTResource // to validate GMT of client and server
@Like // for like filter
public Response like(@HeaderParam("token") @NotNull String token,
@HeaderParam("user-agent") String userAgent,
@NotNull @Valid UserFriendsBaseModel userFriendsBaseModel)
throws BadRequestException, UnauthorizedException,
ForbiddenException, InternalServerError {
userService.like(userFriendsBaseModel,userAgent);
return Response.ok().build();
}

异常:

    javax.servlet.ServletException: org.glassfish.jersey.server.ContainerException: java.io.IOException: Stream closed
org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:419)
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:381)
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:344)
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:221)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause

org.glassfish.jersey.server.ContainerException: java.io.IOException: Stream closed
org.glassfish.jersey.servlet.internal.ResponseWriter.rethrow(ResponseWriter.java:256)
org.glassfish.jersey.servlet.internal.ResponseWriter.failure(ResponseWriter.java:238)
org.glassfish.jersey.server.ServerRuntime$Responder.process(ServerRuntime.java:480)
org.glassfish.jersey.server.ServerRuntime$2.run(ServerRuntime.java:311)
org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
org.glassfish.jersey.internal.Errors.process(Errors.java:315)
org.glassfish.jersey.internal.Errors.process(Errors.java:297)
org.glassfish.jersey.internal.Errors.process(Errors.java:267)
org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:317)
org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:286)
org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1072)
org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:399)
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:381)
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:344)
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:221)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause

java.io.IOException: Stream closed
org.apache.catalina.connector.InputBuffer.read(InputBuffer.java:312)
org.apache.catalina.connector.CoyoteInputStream.read(CoyoteInputStream.java:200)
org.glassfish.jersey.message.internal.EntityInputStream.read(EntityInputStream.java:101)
org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$UnCloseableInputStream.read(ReaderInterceptorExecutor.java:301)
com.fasterxml.jackson.core.json.ByteSourceJsonBootstrapper.ensureLoaded(ByteSourceJsonBootstrapper.java:503)
com.fasterxml.jackson.core.json.ByteSourceJsonBootstrapper.detectEncoding(ByteSourceJsonBootstrapper.java:129)
com.fasterxml.jackson.core.json.ByteSourceJsonBootstrapper.constructParser(ByteSourceJsonBootstrapper.java:224)
com.fasterxml.jackson.core.JsonFactory._createParser(JsonFactory.java:1242)
com.fasterxml.jackson.core.JsonFactory.createParser(JsonFactory.java:753)
com.fasterxml.jackson.jaxrs.base.ProviderBase._createParser(ProviderBase.java:791)
com.fasterxml.jackson.jaxrs.base.ProviderBase.readFrom(ProviderBase.java:760)
org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$TerminalReaderInterceptor.invokeReadFrom(ReaderInterceptorExecutor.java:259)
org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$TerminalReaderInterceptor.aroundReadFrom(ReaderInterceptorExecutor.java:235)
org.glassfish.jersey.message.internal.ReaderInterceptorExecutor.proceed(ReaderInterceptorExecutor.java:155)
org.glassfish.jersey.server.internal.MappableExceptionWrapperInterceptor.aroundReadFrom(MappableExceptionWrapperInterceptor.java:74)
org.glassfish.jersey.message.internal.ReaderInterceptorExecutor.proceed(ReaderInterceptorExecutor.java:155)
org.glassfish.jersey.message.internal.MessageBodyFactory.readFrom(MessageBodyFactory.java:1075)
org.glassfish.jersey.message.internal.InboundMessageContext.readEntity(InboundMessageContext.java:853)
org.glassfish.jersey.server.ContainerRequest.readEntity(ContainerRequest.java:270)
org.glassfish.jersey.server.internal.inject.EntityParamValueFactoryProvider$EntityValueFactory.provide(EntityParamValueFactoryProvider.java:96)
org.glassfish.jersey.server.spi.internal.ParameterValueHelper.getParameterValues(ParameterValueHelper.java:81)
org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$AbstractMethodParamInvoker.getParamValues(JavaResourceMethodDispatcherProvider.java:125)
org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$ResponseOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:158)
org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:97)
org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:389)
org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:347)
org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:102)
org.glassfish.jersey.server.ServerRuntime$2.run(ServerRuntime.java:303)
org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
org.glassfish.jersey.internal.Errors.process(Errors.java:315)
org.glassfish.jersey.internal.Errors.process(Errors.java:297)
org.glassfish.jersey.internal.Errors.process(Errors.java:267)
org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:317)
org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:286)
org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1072)
org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:399)
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:381)
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:344)
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:221)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

我还将请求过滤器的优先级设置为:

@Priority(200)
public class LikeRequestFilter implements ContainerRequestFilter {

private final Logger _logger = LoggerFactory.getLogger(LikeRequestFilter.class);

@Inject
ResourceInfo resourceInfo;

@Inject
UriInfo uriInfo;

@Autowired
UserManager userManager;

@Override
public void filter(ContainerRequestContext requestContext) throws IOException {
if (resourceInfo.getResourceMethod().isAnnotationPresent(Like.class)) {
try {
ObjectMapper jsonMapper = new ObjectMapper();
final UserFriendsBaseModel usm = jsonMapper
.readValue(requestContext.getEntityStream(), UserFriendsBaseModel.class);
boolean isLiked = userManager.isLiked(usm);
/**
* If already liked (on api server db ) the check for other conditions
* otherwise consider as first time like request and send to resource for processing
*/
if(isLiked) {
/**
* Check if already liked on openfire
*/
boolean isRoster = userManager.isRoster(usm);
/**
* if already roster then check for reverse like
* else add entry in add roster failure request
*/
if(isRoster) {
/**
* Check if reverse like on api server if liked then check for openfire
*/
final UserFriendsBaseModel reverseModel = UserFriendsBaseModel.reverse(usm);
boolean isReverseLiked = userManager.isLiked(reverseModel);
/**
* Reverse liked(Friends) on api server db . Check on openfire
*/
if(isReverseLiked) {
boolean isReverseRoster = userManager.isRosterFriend(reverseModel);
/**
* If friend on openfire then send push to both user for new Match
*/
if(isReverseRoster) {
// TODO send push
} else {
userManager.addRosterFailureRequest(reverseModel);
}
}

} else {
userManager.addRosterFailureRequest(usm);
}

requestContext.abortWith(Response.ok().build());
}


} catch (Exception e) {
_logger.error("Something went wrong in Like request Filter", e);
}

_logger.info("end of if in LIKE Filter : ");
}
_logger.info("End of LIKE Filter : ");
}

}

对于 AuthorizationRequestFilter 过滤器为:

@Priority(100)
public class AuthorizationRequestFilter implements ContainerRequestFilter {

private final Logger _logger = LoggerFactory
.getLogger(AuthorizationRequestFilter.class);

@Inject
ResourceInfo resourceInfo;

@Inject
UriInfo uriInfo;

@Autowired
AuthorizationService authorizationService;

和响应过滤器为:

public class LoggingResponseFilter implements ContainerResponseFilter {

final static Logger _logger = Logger.getLogger(LoggingResponseFilter.class
.getName());

public void filter(ContainerRequestContext requestContext,ContainerResponseContext responseContext) throws IOException {
String method = requestContext.getMethod();

_logger.info("Requesting : " + method + " for path " + requestContext.getUriInfo().getPath());

然后 LikeRequestFilterAuthorizationRequestFilter 之后调用过滤器链时出现问题。

我无法找出问题所在?如何链接请求过滤器和响应过滤器?

最佳答案

问题出在 LikeRequestFilter 上。您正在使用 ObjectMapper 读取请求实体 InputStream。一般InputStream只能被读取一次,然后就是空的。因此,在 ObjectMapper 读取流后,它会关闭它。所以当 Jersey 试图读取它时,它不能,因为它是关闭的。即使它没有关闭,它仍然是空的。

对于这个用例,Jersey 允许我们缓冲流,因此它可以被多次读取。不过,您需要做的是将 ContainerRequestContext 转换为 ContainerRequest,它有一个 bufferEntity() 方法。它还具有帮助读取流的方法,如 readEntity,其用法类似于客户端 Response#readEntity()

所以你可以做类似的事情

@Override
public void filter(ContainerRequestContext requestContext) throws IOException {
try {
ContainerRequest cr = (ContainerRequest) requestContext;
cr.bufferEntity();
final UserFriendsBaseModel bm = cr.readEntity(UserFriendsBaseModel.class);

关于java - org.glassfish.jersey.server.ContainerException : java. io.IOException:流关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35479570/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com