You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

自定义注解中动态值与RequestMapping默认值的拼接实现问题

实现自定义注解的路径拼接功能

要实现默认路径/api/contracts与传入动态值的拼接,你需要调整自定义注解的实现逻辑,通过扩展Spring的请求映射处理器完成路径组合,具体步骤如下:

1. 修改自定义注解定义

移除注解上直接指定的@RequestMapping(value = "/api/contracts"),保留空的@RequestMapping元注解,同时保留原有的value属性定义:

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@RequestMapping
public @interface IfnContractRequestMapping {
    String[] value() default {};
}

2. 自定义请求映射处理器

创建RequestMappingHandlerMapping的子类,重写类级映射解析逻辑,实现默认路径与用户传入路径的拼接:

import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
import org.springframework.web.servlet.mvc.condition.*;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;

public class ContractRequestMappingHandlerMapping extends RequestMappingHandlerMapping {

    private static final String DEFAULT_BASE_PATH = "/api/contracts";

    @Override
    protected RequestMappingInfo getMappingForClass(Class<?> handlerType) {
        RequestMappingInfo originalMapping = super.getMappingForClass(handlerType);
        if (originalMapping == null) {
            return null;
        }

        IfnContractRequestMapping contractAnnotation = handlerType.getAnnotation(IfnContractRequestMapping.class);
        if (contractAnnotation == null) {
            return originalMapping;
        }

        String[] userPaths = contractAnnotation.value();
        String[] combinedPaths;

        if (userPaths.length == 0) {
            // 未传入路径时使用默认值
            combinedPaths = new String[]{DEFAULT_BASE_PATH};
        } else {
            // 拼接默认路径与用户传入路径,处理斜杠避免重复
            combinedPaths = new String[userPaths.length];
            for (int i = 0; i < userPaths.length; i++) {
                String userPath = userPaths[i];
                if (!userPath.startsWith("/")) {
                    userPath = "/" + userPath;
                }
                combinedPaths[i] = DEFAULT_BASE_PATH + userPath;
            }
        }

        // 重新构建RequestMappingInfo,替换路径并保留原有其他条件
        return new RequestMappingInfo(
                originalMapping.getName(),
                getPathMatcher().parsePaths(combinedPaths),
                new RequestMethodsRequestCondition(originalMapping.getMethods()),
                new ParamsRequestCondition(originalMapping.getParams()),
                new HeadersRequestCondition(originalMapping.getHeaders()),
                new ConsumesRequestCondition(originalMapping.getConsumes()),
                new ProducesRequestCondition(originalMapping.getProduces()),
                originalMapping.getCustomCondition()
        );
    }
}

3. 配置自定义处理器

通过配置类将自定义的RequestMappingHandlerMapping注册到Spring容器中:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

@Configuration
public class WebMvcConfig {

    @Bean
    public RequestMappingHandlerMapping requestMappingHandlerMapping() {
        return new ContractRequestMappingHandlerMapping();
    }
}

使用效果

  • 当使用@IfnContractRequestMapping("/something")时,最终请求路径为/api/contracts/something
  • 当仅使用@IfnContractRequestMapping时,默认请求路径为/api/contracts

内容的提问来源于stack exchange,提问作者Kaustubh Wani

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 12:44:59