gpt4 book ai didi

java - 如何在 Spring Boot 2 中添加自定义 ArgumentResolvers 而不启用 Web MVC?

转载 作者:行者123 更新时间:2023-12-02 10:37:32 25 4
gpt4 key购买 nike

Here是关于热添加 ArgumentResolver 的示例。它使用以下代码:

@Configuration
public class WebConfig implements WebMvcConfigurer {

@Override
public void addArgumentResolvers(
List<HandlerMethodArgumentResolver> argumentResolvers) {
argumentResolvers.add(new HeaderVersionArgumentResolver());
}
}

但我只使用 @RestController ,没有任何 MVC spring 功能。但需要重用一些逻辑来提取原始请求数据。有没有办法在 Spring Boot 2 应用程序中添加 ArgumentResolver 而不实现 WebMvcConfigurer 接口(interface)?

最佳答案

这根本不可能:

But I use just @RestControllers without any MVC spring features

我怀疑您只是想增强 Spring Boot MVC,而不影响现有的。

来自String Boot documentation :

If you want to keep Spring Boot MVC features and you want to add additional MVC configuration (interceptors, formatters, view controllers, and other features), you can add your own @Configuration class of type WebMvcConfigurer but without @EnableWebMvc.

因此,如果您添加该实现,您将不会失去 Spring Boot MVC 功能。只要您不添加 @EnableWebMvc,就没有理由不实现 WebMvcConfigurer

关于java - 如何在 Spring Boot 2 中添加自定义 ArgumentResolvers 而不启用 Web MVC?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53169160/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com