英文:
SpringBoot functional Web MVC, missing a way to return CompletableFuture<ResponseEntity<String>>
问题
我有一个正在运行的生产SpringBoot应用程序,其中的一部分需要重新设计。对我来说,删除旧的@RequestMapping
从ResponseEntity<String> foo()
的代码会非常有益,同时在我们尝试通过一个功能开关推出新功能的过程中,保留旧的代码作为重复的代码。所有生产租户都通过我的不再是声明性的foo()
函数,而所有的测试和自动化租户可以开始使用全新的EntityResponse<String> bar()
进行实验。
在我的脑海中,实施这个改变的方式非常清晰:
class Router{
@Bean
RouterFunction<ServerResponse> helloWorldRouterFunction(OldHelloWorldService oldHelloWorldService) {
return RouterFunctions.route()
.route(RequestPredicates.path("/helloWorld/{option}"), x ->
{
String option = x.pathVariable("option");
if (FeatureManager.isActive()) {
return ServerResponse.ok().body(String.format("New implementation of Hello World! your option is: %s", option));
} else {
// FutureServerResponse is my own bad implementation of the ServerResponse interface
return FutureServerResponse.from(oldHelloWorldService.futureFoo(Integer.parseInt(option)));
}
}
)
.build();
}
}
这是OldHelloWorldService::futureFoo
的实现:
@RestController
static class OldHelloWorldService {
@RequestMapping("/specialCase")
ResponseEntity<String> specialCase() {
// 一些业务逻辑
return ResponseEntity.ok().body("Special case for Hello World with option 2");
}
/**
* 旧的声明性实现,通过功能性的{@link ServerRouteConfiguration}进行路由,以便根据{@link FeatureManager#isActive()}进行动态选择
* <p>
* 正如你所看到的,在改变之前,这个函数是一个{@link RequestMapping},它处理了
* completable future,我们可以返回具有主体的具体OK响应,以及具有位置的FOUND响应。
*/
// @RequestMapping("/helloWorld/{option}")
CompletableFuture<ResponseEntity<String>> futureFoo(
// @PathVariable
int option) {
return CompletableFuture.supplyAsync(() -> {
if (option == 2) {
return ResponseEntity.status(HttpStatus.FOUND)
.location(URI.create("/specialCase"))
.build();
} else {
return ResponseEntity.ok().body(String.format("Old implementation of Hello World! your option is: %s", option));
}
});
}
}
这个功能允许我的后端代码在将来决定发送哪种ResponseEntity
。正如你所看到的,一个智能函数可以决定是显示带有OK状态的字符串消息,还是提供Location
标头,并声明FOUND状态,甚至根本不提供字符串主体。因为结果类型是完全流畅的ResponseEntity,所以我有能力做我想做的事情。
现在,使用EntityResponse
,你仍然可以使用CompletionStage,但只作为实际实体。在构建EntityResponse
时,我需要给它一个明确的final
状态。如果是OK的话,我不能在我的CompletionStage
运行完之后决定它将是FOUND。
以上代码的唯一问题是,org.springframework.web.servlet.function
不包含我需要的FutureServerResponse
实现。我创建了自己的实现,它能正常工作,但感觉有点巧合,我不想在生产代码中使用它。
现在我感觉这个功能应该仍然存在于某个地方,为什么没有一个FutureServerResponse可以在将来决定它是什么?也许有一种解决这个问题的方法,可以(滥用)使用视图?
可能不是那么明显的是...我正在考虑切换到响应式和WebFlux,但改变整个运行时对当前生产租户会产生更大的影响,并且在功能开关的情况下进行这种转换是不可能的,因为URL必须在MVC和Flux之间共享。
这是一个小众问题,功能性Web MVC资源有限,所以我会非常感谢任何帮助。
我为这个问题创建了一个GitHub伴侣。
英文:
I have a working production SpringBoot application, and part of it is getting a do-over. It would be very beneficial for me to delete my old @RequestMapping
from the ResponseEntity<String> foo()
s of my world, keeping the old code as an as a duplicate while we try to roll out the new functionality behind a feature gate.. All production tenants go through my no-longer-declarative foo()
function, while all my test and automation tenants can start to tinker with a brand new EntityResponse<String> bar()
.
The way to implement the change was so clear in my mind:
class Router{
@Bean
RouterFunction<ServerResponse> helloWorldRouterFunction(OldHelloWorldService oldHelloWorldService) {
return RouterFunctions.route()
.route(RequestPredicates.path("/helloWorld/{option}"), x ->
{
String option = x.pathVariable("option");
if (FeatureManager.isActive()) {
return ServerResponse.ok().body(String.format("New implementation of Hello World! your option is: %s", option));
} else {
// FutureServerResponse is my own bad implementation of the ServerResponse interface
return FutureServerResponse.from(oldHelloWorldService.futureFoo(Integer.parseInt(option)));
}
}
)
.build();
}
}
Here's the implementation for OldHelloWorldService::futureFoo
@RestController
static class OldHelloWorldService {
@RequestMapping("/specialCase")
ResponseEntity<String> specialCase() {
// some business logic
return ResponseEntity.ok().body("Special case for Hello World with option 2");
}
/**
* Old declarative implementation, routed via functional {@link ServerRouteConfiguration}
* to allow dynamic choice based on {@link FeatureManager#isActive()}
* <p>
* as you can see, before the change, this function was a {@link RequestMapping} and it handled the
* completable future, we could return both concrete OK responses with a body, and FOUND responses with a location.
*/
// @RequestMapping("/helloWorld/{option}")
CompletableFuture<ResponseEntity<String>> futureFoo(
// @PathVariable
int option) {
return CompletableFuture.supplyAsync(() -> {
if (option == 2) {
return ResponseEntity.status(HttpStatus.FOUND)
.location(URI.create("/specialCase"))
.build();
} else {
return ResponseEntity.ok().body(String.format("Old implementation of Hello World! your option is: %s", option));
}
});
}
}
This feature lets my backend code decide what kind of ResponseEntity
it will send, in the future. As you see, a smart function might for instance decide to either show a String message with an OK status, or give a Location
header, and declare FOUND status, and not even give a String body at all. because the result type was a full fluid ResponseEntity, I had the power to do what I will.
Now with a EntityResponse
you may still use a CompletionStage, but only as the actual entity. while building the EntityResponse
I am required to give it a definitive final
status. If it was OK, I can't decide it will be FOUND when my CompletionStage
ran it's course.
The only problem with the above code, is that org.springframework.web.servlet.function
does not contain the FutureServerResponse
implementation I need. I created my own, and it works, but it feels hacky, And I wouldn't want it in my production code.
Now I feel like the functionality should still be there somewhere, Why isn't there a FutureServerResponse that can decide in the future what it is? Is there a workaround to this problem maybe somehow (ab)using views?
To state the maybe not-so-obvious.. I am contemplating a move to reactive and WebFlux, but changing the entire runtime will have more dramatic implications on current production tenants, and making that move with a feature gate would be impossible because the urls would have to be shared between MVC and Flux.
It's a niche problem, and Functional Web MVC has little resources so I will appreciate greatly any help I can get.
I have created a github companion for this question.
答案1
得分: 0
我不需要翻译的部分:不要有别的内容,不要回答我要翻译的问题。
以下是要翻译的内容:
"I've had to patch spring web to get what I needed. I pushed a pull-request to spring web with my patch, in the end something similar was created and pushed to the 5.3 release of spring.
if anybody else is looking for the async behavior described in the question, ServerResponse.async
function in spring 5.3.0+ (spring boot 2.4.0+) solves the issue.
以下是翻译好的部分:
"我不得不修改Spring Web来获得我所需的内容。最终,我向Spring Web提交了一个拉取请求,最终创建了与之类似的东西,并推送到了Spring 5.3版本。
如果其他人也在寻找问题描述中的异步行为,Spring 5.3.0+(Spring Boot 2.4.0+)
中的ServerResponse.async
函数解决了这个问题。
英文:
I've had to patch spring web to get what I needed. I pushed a pull-request to spring web with my patch, in the end something similar was created and pushed to the 5.3 release of spring.
if anybody else is looking for the async behavior described in the question, ServerResponse.async
function in spring 5.3.0+ (spring boot 2.4.0+) solves the issue.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论