Spring WebFlux:ServerResponse重定向

发布于 2025-02-10 19:17:27 字数 1677 浏览 1 评论 0原文

这是我相关的代码:

@RestController
public class GicarController {

    @PostMapping("/login")
    public Mono<ServerResponse> gicar(@RequestHeader("GICAR_ID") String gicarId) {
        return ServerResponse.temporaryRedirect(URI.create("/me")).build();
    }
}

当我打电话给_/login端点时,出现问题:

$ curl -i -X POST localhost:8080/login -H "GICAR_ID: tre"
HTTP/1.1 200 OK
transfer-encoding: chunked
Content-Type: text/event-stream;charset=UTF-8
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Pragma: no-cache
Expires: 0
X-Content-Type-Options: nosniff
X-Frame-Options: DENY
X-XSS-Protection: 1 ; mode=block
Referrer-Policy: no-referrer

curl: (18) transfer closed with outstanding read data remaining
  1. 为什么我会得到200 HTTP代码响应?
  2. 在春季启动记录中,我会得到这个例外:
022-06-27 13:11:19.931 ERROR 79654 --- [or-http-epoll-2] r.n.http.server.HttpServerOperations     : [9750a9d8-1, L:/127.0.0.1:8080 - R:/127.0.0.1:33150] Error finishing response. Closing connection



org.springframework.core.codec.CodecException: Type definition error: [simple type, class org.springframework.web.reactive.function.server.DefaultServerResponseBuilder$WriterFunctionResponse]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class org.springframework.web.reactive.function.server.DefaultServerResponseBuilder$WriterFunctionResponse and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS)

为什么要重新出现异常?

有什么想法吗?

This is my related code:

@RestController
public class GicarController {

    @PostMapping("/login")
    public Mono<ServerResponse> gicar(@RequestHeader("GICAR_ID") String gicarId) {
        return ServerResponse.temporaryRedirect(URI.create("/me")).build();
    }
}

Issue arises when I'm calling to _/login endpoint:

$ curl -i -X POST localhost:8080/login -H "GICAR_ID: tre"
HTTP/1.1 200 OK
transfer-encoding: chunked
Content-Type: text/event-stream;charset=UTF-8
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Pragma: no-cache
Expires: 0
X-Content-Type-Options: nosniff
X-Frame-Options: DENY
X-XSS-Protection: 1 ; mode=block
Referrer-Policy: no-referrer

curl: (18) transfer closed with outstanding read data remaining
  1. Why am I getting an 200 http code response?
  2. On spring boot logging I'm getting this exception:
022-06-27 13:11:19.931 ERROR 79654 --- [or-http-epoll-2] r.n.http.server.HttpServerOperations     : [9750a9d8-1, L:/127.0.0.1:8080 - R:/127.0.0.1:33150] Error finishing response. Closing connection



org.springframework.core.codec.CodecException: Type definition error: [simple type, class org.springframework.web.reactive.function.server.DefaultServerResponseBuilder$WriterFunctionResponse]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class org.springframework.web.reactive.function.server.DefaultServerResponseBuilder$WriterFunctionResponse and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS)

Why above exception is reaised?

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

无远思近则忧 2025-02-17 19:17:27

根据春季文档serverresponse

表示由处理程序函数或过滤器函数返回的类型服务器端HTTP响应。

它应该在功能端点

@Configuration
public class GicarConfiguration {
    @Bean
    public RouterFunction<ServerResponse> route() {
        return RouterFunctions
                .route(POST("/login"), this::loginHandler);
    }

    private Mono<ServerResponse> loginHandler(ServerRequest request) {
        var gicarId = request.headers().firstHeader("GICAR_ID");
        return ServerResponse.temporaryRedirect(URI.create("/me")).build();
    }
}

如果您仍然想使用注释的控制器 >而是

@RestController
public class GicarController {

    @PostMapping("/login")
    public Mono<ResponseEntity<Void>> gicar() {
        return Mono.just(ResponseEntity
                .status(HttpStatus.TEMPORARY_REDIRECT)
                .header(HttpHeaders.LOCATION, "/me")
                .build()
        );
    }
}

According to Spring documentation ServerResponse

Represents a typed server-side HTTP response, as returned by a handler function or filter function.

and it supposed to be used in Functional Endpoints

@Configuration
public class GicarConfiguration {
    @Bean
    public RouterFunction<ServerResponse> route() {
        return RouterFunctions
                .route(POST("/login"), this::loginHandler);
    }

    private Mono<ServerResponse> loginHandler(ServerRequest request) {
        var gicarId = request.headers().firstHeader("GICAR_ID");
        return ServerResponse.temporaryRedirect(URI.create("/me")).build();
    }
}

If you still want to use Annotated Controllers, use ResponseEntity instead

@RestController
public class GicarController {

    @PostMapping("/login")
    public Mono<ResponseEntity<Void>> gicar() {
        return Mono.just(ResponseEntity
                .status(HttpStatus.TEMPORARY_REDIRECT)
                .header(HttpHeaders.LOCATION, "/me")
                .build()
        );
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文