gpt4 book ai didi

java - 是否可以在@RequestParam中有条件地赋值Required?

转载 作者:行者123 更新时间:2023-11-30 12:08:02 24 4
gpt4 key购买 nike

我的 Controller 中有 2 个@RequestParam 参数。我想根据条件设置两个参数的必需值。条件可能是,如果传递了一个参数,则必须传递另一个参数。所以将 other 的 required 设置为 true ,反之亦然。否则,如果没有传递任何参数,则将两者都设置为 false。

@RestController
public class TestController {

@GetMapping("/test")
public void test(@RequestParam(value = "a", required = (b !=null) String a,
@RequestParam(value = "b", required = (a !=null) ) String b,) {
{

}

}

在@RequestParam() 中使用变量名的语法是错误的,但我想解释一下我想要的。

最佳答案

您可以使用以下两种方式之一来完成:

  1. 使用 Spring AOP 并为该请求创建一个周边方面映射
  2. 使用 HandlerInterceptorAdapter 拦截给定 URI 的请求

<强>1。使用 Spring AOP

创建如下注释:

public @interface RequestParameterPairValidation {

}

然后你可以用它来注解你的请求映射方法:

@GetMapping("/test")
@RequestParameterPairValidation
public void test(
@RequestParam(value = "a", required = false) String a,
@RequestParam(value = "b", required = false) String b) {
// API code goes here...
}

围绕注释创建一个方面。像这样的东西:

@Aspect
@Component
public class RequestParameterPairValidationAspect {
@Around("@annotation(x.y.z.RequestParameterPairValidation) && execution(public * *(..))")
public Object time(final ProceedingJoinPoint joinPoint) throws Throwable {
Object[] requestMappingArgs = joinPoint.getArgs();
String a = (String) requestMappingArgs[0];
String b = (String) requestMappingArgs[1];

boolean requestIsValid = //... execute validation logic here

if (requestIsValid) {
return joinPoint.proceed();
} else {
throw new IllegalArgumentException("illegal request");
}
}
}

请注意,在此处返回 400 BAD REQUEST 是一个不错的选择,因为请求无效。当然,这取决于上下文,但这是开始时的一般经验法则。

<强>2。使用 HandlerInterceptorAdapter

创建一个新的拦截器映射到您想要的 URI(在本例中为 /test):

@Configuration  
public class CustomInterceptor extends WebMvcConfigurerAdapter {

@Override
public void addInterceptors(InterceptorRegistry registry) {
registry
.addInterceptor(new CustomRequestParameterPairInterceptor())
.addPathPatterns("/test");
}
}

在自定义拦截器中定义验证逻辑:

public class CustomRequestParameterPairInterceptor extends HandlerInterceptorAdapter {

@Override
public void afterCompletion(HttpServletRequest req, HttpServletResponse res, Object obj, Exception exception) throws Exception {

}

@Override
public void postHandle(HttpServletRequest req, HttpServletResponse res, Object obj, ModelAndView modelAndView) throws Exception {

}

@Override
public boolean preHandle(HttpServletRequest req, HttpServletResponse res, Object handler) throws Exception {
// Run your validation logic here
}
}

我会说第二个选项是最好的,因为您可以直接控制请求的答案。在这种情况下,它可能是 400 BAD REQUEST,或者其他任何对您的情况更有意义的内容。

关于java - 是否可以在@RequestParam中有条件地赋值Required?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54514014/

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