【问题标题】:io.helidon.webserver.ServerResponse.send() throwing error on server but file downloaded on front endio.helidon.webserver.ServerResponse.send() 在服务器上抛出错误但文件在前端下载
【发布时间】:2021-07-29 13:12:11
【问题描述】:

尝试为服务器中的(文件上传和下载)错误编写一个示例。 虽然它在前端下载文件,但在服务器中出现错误。

对于路由--

WebServer.builder(getRouting()).port(8080).build().start();
    
private static Routing getRouting() throws Exception{
        
        return Routing.builder().register("/filetest", JerseySupport.builder().register(FileController.class).build())
            .build();
    }


@RequestScoped
public class FileController {
     @Context
     ServerRequest req;
     @Context
     ServerResponse res;
    @GET
    @Path("/{fname}")
    public void download(@PathParam("fname") String fname) {
        
        try {
            //Getting the file 
            java.nio.file.Path filepath = Paths.get("c:/"+fname+".txt");
            ResponseHeaders headers = res.headers();
            headers.contentType(io.helidon.common.http.MediaType.APPLICATION_OCTET_STREAM);
            headers.put(Http.Header.CONTENT_DISPOSITION, ContentDisposition.builder()
                    .filename(filepath.getFileName().toString())
                    .build()
                    .toString());
           
           res.send(filepath);         
        }catch(Exception e) {
            
        }
}

2021 年 7 月 29 日下午 6:20:36 io.helidon.webserver.RequestRouting$RoutedRequest defaultHandler 警告:默认错误处理程序:遇到未处理的异常。 java.util.concurrent.ExecutionException:未处理的“原因” 遇到异常。在 io.helidon.webserver.RequestRouting$RoutedRequest.defaultHandler(RequestRouting.java:397) 在 io.helidon.webserver.RequestRouting$RoutedRequest.nextNoCheck(RequestRouting.java:377) 在 io.helidon.webserver.RequestRouting$RoutedRequest.next(RequestRouting.java:420) 在 io.helidon.webserver.jersey.ResponseWriter.failure(ResponseWriter.java:133) 在 org.glassfish.jersey.server.ServerRuntime$Responder.process(ServerRuntime.java:438) 在 org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:263) 在 org.glassfish.jersey.internal.Errors$1.call(Errors.java:248) 在 org.glassfish.jersey.internal.Errors$1.call(Errors.java:244) 在 org.glassfish.jersey.internal.Errors.process(Errors.java:292) 在 org.glassfish.jersey.internal.Errors.process(Errors.java:274) 在 org.glassfish.jersey.internal.Errors.process(Errors.java:244) 在 org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:265) 在 org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:234) 在 org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:680) 在 io.helidon.webserver.jersey.JerseySupport$JerseyHandler.lambda$doAccept$3(JerseySupport.java:299) 在 io.helidon.common.context.Contexts.runInContext(Contexts.java:117) 在 io.helidon.common.context.ContextAwareExecutorImpl.lambda$wrap$5(ContextAwareExecutorImpl.java:154) 在 java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128) 在 java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) 在 java.base/java.lang.Thread.run(Thread.java:834) 引起: io.helidon.common.http.AlreadyCompletedException:响应状态码 并且标头已经完成(发送给客户端)!在 io.helidon.webserver.HashResponseHeaders$CompletionSupport.runIfNotCompleted(HashResponseHeaders.java:384) 在 io.helidon.webserver.HashResponseHeaders.httpStatus(HashResponseHeaders.java:251) 在 io.helidon.webserver.Response.status(Response.java:122) 在 io.helidon.webserver.Response.status(Response.java:48) 在 io.helidon.webserver.jersey.ResponseWriter.writeResponseStatusAndHeaders(ResponseWriter.java:81) 在 org.glassfish.jersey.server.ServerRuntime$Responder.writeResponse(ServerRuntime.java:607) 在 org.glassfish.jersey.server.ServerRuntime$Responder.processResponse(ServerRuntime.java:373) 在 org.glassfish.jersey.server.ServerRuntime$Responder.process(ServerRuntime.java:363) 在 org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:258) ... 14 更多

2021 年 7 月 29 日下午 6:20:36 io.helidon.webserver.RequestRouting$RoutedRequest defaultHandler 警告:无法对 throwable 执行错误处理(请参阅原因 此异常)因为标头已发送 java.lang.IllegalStateException:标头已发送。不能处理 此异常的原因。在 io.helidon.webserver.RequestRouting$RoutedRequest.defaultHandler(RequestRouting.java:405) 在 io.helidon.webserver.RequestRouting$RoutedRequest.nextNoCheck(RequestRouting.java:377) 在 io.helidon.webserver.RequestRouting$RoutedRequest.next(RequestRouting.java:420) 在 io.helidon.webserver.jersey.ResponseWriter.failure(ResponseWriter.java:133) 在 org.glassfish.jersey.server.ServerRuntime$Responder.process(ServerRuntime.java:438) 在 org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:263) 在 org.glassfish.jersey.internal.Errors$1.call(Errors.java:248) 在 org.glassfish.jersey.internal.Errors$1.call(Errors.java:244) 在 org.glassfish.jersey.internal.Errors.process(Errors.java:292) 在 org.glassfish.jersey.internal.Errors.process(Errors.java:274) 在 org.glassfish.jersey.internal.Errors.process(Errors.java:244) 在 org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:265) 在 org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:234) 在 org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:680) 在 io.helidon.webserver.jersey.JerseySupport$JerseyHandler.lambda$doAccept$3(JerseySupport.java:299) 在 io.helidon.common.context.Contexts.runInContext(Contexts.java:117) 在 io.helidon.common.context.ContextAwareExecutorImpl.lambda$wrap$5(ContextAwareExecutorImpl.java:154) 在 java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128) 在 java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) 在 java.base/java.lang.Thread.run(Thread.java:834) 引起: io.helidon.common.http.AlreadyCompletedException:响应状态码 并且标头已经完成(发送给客户端)!在 io.helidon.webserver.HashResponseHeaders$CompletionSupport.runIfNotCompleted(HashResponseHeaders.java:384) 在 io.helidon.webserver.HashResponseHeaders.httpStatus(HashResponseHeaders.java:251) 在 io.helidon.webserver.Response.status(Response.java:122) 在 io.helidon.webserver.Response.status(Response.java:48) 在 io.helidon.webserver.jersey.ResponseWriter.writeResponseStatusAndHeaders(ResponseWriter.java:81) 在 org.glassfish.jersey.server.ServerRuntime$Responder.writeResponse(ServerRuntime.java:607) 在 org.glassfish.jersey.server.ServerRuntime$Responder.processResponse(ServerRuntime.java:373) 在 org.glassfish.jersey.server.ServerRuntime$Responder.process(ServerRuntime.java:363) 在 org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:258) ... 14 更多

【问题讨论】:

  • 混合 Helidon SE 和 Helidon MP 有什么特别的原因吗?

标签: helidon helidon-httpclient helidon-webclient


【解决方案1】:

使用 Helidon MP 时,Jersey 负责发送响应。

在上面的代码中,您使用的是底层 WebServer 并在 JAXRS 资源方法的主体内发送响应,当 Jersey 发送响应时会引发异常,因为它已经发送。

Helidon MP Multipart example

Jersey Multipart documentation

【讨论】:

  • 谢谢罗曼
猜你喜欢
  • 1970-01-01
  • 2021-09-23
  • 2020-12-02
  • 1970-01-01
  • 2020-04-12
  • 2021-04-15
  • 1970-01-01
  • 1970-01-01
  • 2017-10-23
相关资源
最近更新 更多