如何在Spring Integration(DSL)中公开“ Content-Disposition”?

时间:2019-06-29 17:38:31

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

要下载文件,我将“ 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,必须重新验证”
内容类型:“ application / json”
到期:“ 0”
编译指示:“无缓存”

1 个答案:

答案 0 :(得分: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()

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