我如何将请求标头传播到Springboot 2中的响应标头
我有Swagger Codegen生成的接口。看起来像这样:
@PostMapping(value = "/ipc/conf", produces = {"application/json", "application/problem+json"}, consumes = {
"application/json"})
default ResponseEntity<CustomResponseEntity> ipcConfPost(
@ApiParam(value = "ID", required = true) @RequestHeader(value = "X-Request-ID", required = true) String xRequestID,
@ApiParam(value = "Value for identifying a single transaction across multiple services up to the backend.", required = true) @RequestHeader(value = "X-Correlation-ID", required = true) String xCorrelationID,
@ApiParam(value = "The payload to transmit", required = true) @Valid @RequestBody IPcData ipcConfData,
@ApiParam(value = "The business context is a general classification for a larger number of requests.") @RequestHeader(value = "X-Business-Context", required = false) String xBusinessContext) {
getRequest().ifPresent(request -> {
for (MediaType mediaType : MediaType.parseMediaTypes(request.getHeader("Accept"))) {
if (mediaType.isCompatibleWith(MediaType.valueOf("application/json"))) {
String exampleString = "{ \"id\" : \"id\", \"error\" : \"error\" }";
ApiUtil.setExampleResponse(request, "application/json", exampleString);
break;
}
}
});
return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
}
在实施中,我想拥有一个请求标题的完整列表(我需要其中一些在响应中),或者能够获得API中未列出的标头的值。问题是我不能更改端点的签名,因为它将在进一步的版本中引起重大头痛。 那么有什么方法可以实现这一目标吗?
I have interfaces generated by Swagger Codegen. It looks like this:
@PostMapping(value = "/ipc/conf", produces = {"application/json", "application/problem+json"}, consumes = {
"application/json"})
default ResponseEntity<CustomResponseEntity> ipcConfPost(
@ApiParam(value = "ID", required = true) @RequestHeader(value = "X-Request-ID", required = true) String xRequestID,
@ApiParam(value = "Value for identifying a single transaction across multiple services up to the backend.", required = true) @RequestHeader(value = "X-Correlation-ID", required = true) String xCorrelationID,
@ApiParam(value = "The payload to transmit", required = true) @Valid @RequestBody IPcData ipcConfData,
@ApiParam(value = "The business context is a general classification for a larger number of requests.") @RequestHeader(value = "X-Business-Context", required = false) String xBusinessContext) {
getRequest().ifPresent(request -> {
for (MediaType mediaType : MediaType.parseMediaTypes(request.getHeader("Accept"))) {
if (mediaType.isCompatibleWith(MediaType.valueOf("application/json"))) {
String exampleString = "{ \"id\" : \"id\", \"error\" : \"error\" }";
ApiUtil.setExampleResponse(request, "application/json", exampleString);
break;
}
}
});
return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
}
In the implementation I want to have a full list of request headers (I need some of them in the response) or to be able to get a value of a header that is not listed in the API. The thing is I cannot change the signature of the endpoint since it will cause a major headache in further releases.
So is there any way to achieve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您已经在代码中拥有请求对象,因此您可以从中获取标题。 IE
request.getheadernames()
然后循环通过它们。之后,您可以将它们添加到响应中
You have the request object in your code already so you can get the headers from it. i.e.
request.getHeaderNames()
then loop through them.After that, you can add them to the response with