【问题标题】:How can I expose the "Content-Disposition" in spring-integration (DSL)?如何在 spring-integration (DSL) 中公开“Content-Disposition”?
【发布时间】:2019-06-29 17:38:31
【问题描述】:

为了下载文件,我会将“Content-Disposition”添加到我的 responseHeader 中,但它不起作用。

响应不会有任何附加属性。

    @Bean
    public ExpressionParser fileParser() {
        return new SpelExpressionParser();
    }

    @Bean
    public HeaderMapper<HttpHeaders> fileHeaderMapper() {
        return new DefaultHttpHeaderMapper();
    }
@Bean
    public IntegrationFlow httpGetFileDownload() {
        return IntegrationFlows.from(
                Http.inboundGateway("/api/files/download/{id}")
                        .requestMapping(r -> r.methods(HttpMethod.GET))
                        .statusCodeExpression(fileParser().parseExpression("T(org.springframework.http.HttpStatus).BAD_REQUEST"))
                        .payloadExpression(fileParser().parseExpression("#pathVariables.id"))
                        .crossOrigin(cors -> cors.origin("*").exposedHeaders("Content-Disposition", "content-disposition"))
                        .headerMapper(fileHeaderMapper())
                )
                .channel("http.file.download.channel")
                .handle("fileEndpoint", "download")
                .get();
    }



public Message<?> download(Message<Long> msg){
...

return MessageBuilder
                    .withPayload(resource)
                    .copyHeaders(msg.getHeaders())
                    .setHeader(STATUSCODE_HEADER, HttpStatus.OK)
                    .setHeader(HttpHeaders.CONTENT_DISPOSITION,"attachment;filename=" + file.getName())
                    .setHeader(HttpHeaders.CONTENT_TYPE, mimeType)
                    .setHeader(HttpHeaders.CONTENT_LENGTH, (int)file.length())
                    .build();
}


我得到了什么:

缓存控制:“无缓存,无存储,最大年龄=0,必须重新验证”
内容类型:“应用程序/json”
过期:“0”
编译指示:“无缓存”

【问题讨论】:

  • 嗨 Ryan,您尝试使用 ContentDisposition.builder() 吗?
  • 没有。我应该在哪里绑定这个方法?在我的处理程序中?
  • 创建您的 HttpHeaders.CONTENT_DISPOSITION
  • 那行不通
  • 你的fileHeaderMapper 是什么? DefaultHttpHeaderMapperHttpHeaders.CONTENT_DISPOSITION 映射到 HTTP 响应中。所以,我们需要知道你的那个是做什么的。另一方面,如果您有一些简单的项目可以与我们分享以进行调试,那就太好了。

标签: spring-integration spring-integration-dsl spring-integration-http


【解决方案1】:

DefaultHttpHeaderMapper 默认为空的问题。我认为现在可能是时候将 ctor 设为 deprecated 以不允许从最终应用程序中使用它。 或者进行一些验证以拒绝只是空的(未配置)DefaultHttpHeaderMapper...

如果您不自定义它,那么使用 return new DefaultHttpHeaderMapper(); 的意义何在。 HttpRequestHandlingMessagingGateway中有一个默认的:

private HeaderMapper<HttpHeaders> headerMapper = DefaultHttpHeaderMapper.inboundMapper();

要解决您的问题,您肯定需要使用 inboundMapper() 工厂方法,它会这样做:

/**
 * Factory method for creating a basic inbound mapper instance.
 * This will map all standard HTTP request headers when receiving an HTTP request,
 * and it will map all standard HTTP response headers when sending an HTTP response.
 * @return The default inbound mapper.
 */
public static DefaultHttpHeaderMapper inboundMapper() {
    DefaultHttpHeaderMapper mapper = new DefaultHttpHeaderMapper();
    setupDefaultInboundMapper(mapper);
    return mapper;
}

setupDefaultInboundMapper() 非常重要:它为我们带来了一组从请求映射到响应的标头。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-11
    • 1970-01-01
    • 2017-12-23
    • 2017-12-22
    • 2018-03-24
    • 1970-01-01
    相关资源
    最近更新 更多