当前位置: 首页 > 知识库问答 >
问题:

如何向com.myproject.api下的所有控制器添加“api”前缀?

夹谷腾
2023-03-14

我试图找到它,但我发现了许多不同的场景,但不是这个。

我要做的是将“/api/”前缀添加到com.myproject.api下控制器中的所有路由。我希望com.myapp.api包下的所有控制器都使用“/api/*”,com.myapp.web包下的所有控制器都不使用前缀

是否可以使用Spring/Spring靴?

共有3个答案

姚臻
2023-03-14

对于Spring Boot,这对我来说很有用:

@Configuration
@EnableWebMvc
public class WebMvcConfiguration implements WebMvcConfigurer {

    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
        configurer.addPathPrefix("/api",
               HandlerTypePredicate.forBasePackage("com.your.package"));
    }
}
何安宜
2023-03-14

我通过以下方式实现了我认为你正在寻找的结果,只要你使用MVC。

首先做一个配置类,实现WebMvc注册

@Configuration
public class WebMvcConfig implements WebMvcRegistrations {

    @Value("${Prop.Value.String}") //"api"
    private String apiPrefix;

    @Value("${Prop.Package.Names}") //["com.myapp.api","Others if you like"]
    private String[] prefixedPackages;

    @Override
    public RequestMappingHandlerMapping getRequestMappingHandlerMapping() {
        return new PrefixedApiRequestHandler(apiPrefix,prefixedPackages);
    }
}

然后创建一个类,该类扩展UrestMappingHandlerMap并覆盖getMappingFormethod

@Log4j2
public class PrefixedApiRequestHandler extends RequestMappingHandlerMapping {

    private final String prefix;

    private final String[] prefixedPackages;

    public PrefixedApiRequestHandler(final String prefix, final String... packages) {
        super();
        this.prefix = prefix;
        this.prefixedPackages = packages.clone();

    }

    @Override
    protected RequestMappingInfo getMappingForMethod(final Method method, final Class<?> handlerType) {

        RequestMappingInfo info = super.getMappingForMethod(method, handlerType);
        if (info == null) {
            return null;
        }

        for (final String packageRef : prefixedPackages) {
            if (handlerType.getPackageName().contains(packageRef)) {
                info = createPrefixedApi().combine(info);

                log.trace("Updated Prefixed Mapping " + info);
                return info;
            }
        }
        log.trace("Skipped Non-Prefixed Mapping " + info);
        return info;
    }

    private RequestMappingInfo createPrefixedApi() {
        String[] patterns = new String[prefix.length()];
        for (int i = 0; i < patterns.length; i++) {
            // Build the URL prefix
            patterns[i] = prefix;
        }

        return new RequestMappingInfo(
                new PatternsRequestCondition(patterns,
                        getUrlPathHelper(),
                        getPathMatcher(),
                        useSuffixPatternMatch(),
                        useTrailingSlashMatch(),
                        getFileExtensions()),
                new RequestMethodsRequestCondition(),
                new ParamsRequestCondition(),
                new HeadersRequestCondition(),
                new ConsumesRequestCondition(),
                new ProducesRequestCondition(),
                null);
    }
}

然后,您应该只在指定的包中看到所有映射的/api/(ControllerMapping)。注意:我的控制器顶部有@RequestMapping(“/”)。

梅宏盛
2023-03-14

如果您使用的是springboot,您可以添加以下内容:

server.servlet.context-path=/api

到application.properties文件。

 类似资料:
  • 我试图找到它,但我发现了很多不同的场景,但没有这一个。 我想做的是在com.myproject.api下的控制器中的所有路由中添加“/api/”前缀。我希望com.myapp.api包下的所有控制器使用“/api/*”,而com.myapp.web包下的所有控制器不使用前缀 用Spring/Spring靴可以吗?

  • 我正试图提供格式化的描述给一个在大摇大摆的控制器。但是,我找不到任何可以启用它的注释。当前看起来如下所示: 然而,我想提供有意义的描述。不推荐使用带有的参数。

  • 问题内容: 我知道您可以设置in 来更改根上下文。 另外,我可以在Spring Boot的应用程序配置中添加一个附加上下文,如以下示例(在Groovy中),以便在根上下文的URL映射中添加“ / api”: 我正在尝试为Web服务调用专门准备一个单独的基本URI“ / api”,我可以利用它来提高安全性,等等。但是,使用上述方法将意味着可以通过以下方式访问我的任何URI(无论是否为Web服务) “

  • 我的第一个帖子,请温柔点。 我正在为一个简单的游戏制作一个JavaFX图形用户界面。我有一个FXML布局加载通过FXML文件加载器在这里: 然后我在Main.java创建一个按钮: 以及为了显示: 问题:我究竟如何将FXML布局和普通java按钮对象组合在同一场景中?它像苹果和桔子一样,不可能结合吗?我的IDE不允许我调用java文档中推荐的方法: 等等 请帮我和/或给我指个方向,我真的试过了。。

  • 问题内容: 我有控制器映射和: 我想分别通过URL 和访问这些URL 。 如何在Spring Boot中实现这一目标? 问题答案: 您可以在自定义配置中提供到Spring Boot应用程序的根上下文路径的映射。 或将此添加到您的in 文件夹中 编辑 从Spring Boot 2.x开始,该属性已被弃用,应替换为 您可以在此处找到更多的内容Spring Boot Context Root, 并在此处

  • 我想通过URL分别访问和。 如何在Spring Boot中实现这一点?