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

问题描述 投票:0回答:4

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

我想要做的是在com.myproject.api下的控制器中的所有路由中添加“/ api /”前缀。对于com.myapp.api包下的所有控制器,我想要“/ api / *”,com.myapp.web下的所有控制器都没有前缀

是否可以使用Spring / Spring Boot?

java spring spring-mvc spring-boot spring-webflow
4个回答
1
投票

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

server.servlet.context-path=/api

该applikation.properties好友。


1
投票

只要您使用MVC,我就会以下列方式实现我认为您正在寻找的结果。

首先创建一个实现WebMvcRegistrations的配置类

@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);
    }
}

然后创建一个扩展RequestMappingHandlerMapping并覆盖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("/")


0
投票

已经回答了herehere

在src / main / resources下添加application.properties文件,使用以下选项:

server.contextPath=/api

查看common properties的官方参考。


0
投票

你应该将@RequestMapping("/api")添加到每个所需的@Controller@RestController类的顶部。

当类和方法都有该注释时,Spring Boot会在构建url时附加它们。在下面的例子中,该方法将绑定到/api/user路线。

@RequestMapping("/api")
@RestController
public class MyController {

  @RequestMapping("/user")
  public List<User> getUsers(){
     return ...
  }

}
© www.soinside.com 2019 - 2024. All rights reserved.