如何在 spring 集成 (DSL) 中公开 "Content-Disposition"?
How can I expose the "Content-Disposition" in spring-integration (DSL)?
为了下载文件,我会将 "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();
}
我得到的:
缓存控制:"no-cache, no-store, max-age=0, must-revalidate"
内容类型:"application/json"
过期:“0”
编译指示:"no-cache"
你的问题 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()
非常重要:它为我们带来了一组 headers 从请求映射到响应。
为了下载文件,我会将 "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();
}
我得到的:
缓存控制:"no-cache, no-store, max-age=0, must-revalidate"
内容类型:"application/json"
过期:“0”
编译指示:"no-cache"
你的问题 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()
非常重要:它为我们带来了一组 headers 从请求映射到响应。