如何在弹簧集成 (DSL) 中公开"Content-Disposition"?



用于下载文件,我会将" content-disposition"添加到我的响应状态下,但它不起作用。

响应将没有任何添加的属性。

    @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();
}

我得到的:

cache-control:" no-cache,no-store,max-age = 0,必备"
内容类型:"应用程序/JSON"
到期:" 0"
PRAGMA:"无孔"

默认情况下DefaultHttpHeaderMapper是空的问题。我认为现在可能是将CTOR作为deprecated的时候,不允许从END应用程序中使用它。或进行一些验证以拒绝空的(未配置(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()非常重要:它为我们带来了一组从请求映射到响应的标题。

最新更新