我如何将请求标头传播到Springboot 2中的响应标头

发布于 2025-02-03 15:18:09 字数 1626 浏览 3 评论 0原文

我有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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

假装爱人 2025-02-10 15:18:09

您已经在代码中拥有请求对象,因此您可以从中获取标题。 IE request.getheadernames()然后循环通过它们。

之后,您可以将它们添加到响应中

HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.set("key", "value");
ResponseEntity.ok().headers(responseHeaders).body("some body");

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

HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.set("key", "value");
ResponseEntity.ok().headers(responseHeaders).body("some body");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文