3 回答

TA貢獻(xiàn)1862條經(jīng)驗(yàn) 獲得超6個(gè)贊
您可以使用以下 2 種方式之一來執(zhí)行此操作:
使用 Spring AOP 并為該請求映射創(chuàng)建一個(gè)環(huán)繞方面
使用 HandlerInterceptorAdapter 攔截給定 URI 的請求
1.使用Spring AOP
創(chuàng)建如下注釋:
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...
}
圍繞注釋創(chuàng)建一個(gè)方面。就像是:
@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請注意,由于請求無效,因此返回此處是一個(gè)不錯(cuò)的選擇。當(dāng)然,這取決于上下文,但這是一般的經(jīng)驗(yàn)法則。
2.使用HandlerInterceptorAdapter
創(chuàng)建一個(gè)新的攔截器映射到您想要的 URI(在本例中/test):
@Configuration
public class CustomInterceptor extends WebMvcConfigurerAdapter {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry
.addInterceptor(new CustomRequestParameterPairInterceptor())
.addPathPatterns("/test");
}
}
在自定義攔截器中定義驗(yàn)證邏輯:
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
}
}
我會(huì)說第二個(gè)選項(xiàng)是最好的,因?yàn)槟梢灾苯涌刂普埱蟮拇鸢浮T谶@種情況下,它可能是 a400 BAD REQUEST或任何其他對您的情況更有意義的東西。

TA貢獻(xiàn)1805條經(jīng)驗(yàn) 獲得超10個(gè)贊
您可以在這里以智能的方式使用 Optional,如下所示:
@GetMapping("/test")
@RequestParameterPairValidation
public void test(@RequestParam("a") Optional<String> a,
@RequestParam("b") Optional<String> b){
String aVal = a.isPresent() ? a.get() : null;
String bVal = b.isPresent() ? b.get() : null;
//go for service call here based on your logic
}
我希望這可以滿足您的要求。

TA貢獻(xiàn)1827條經(jīng)驗(yàn) 獲得超9個(gè)贊
您可以在 Spring 中使用 Java EE @Size Validation 注解(但您必須在類路徑上有一個(gè) Java EE 驗(yàn)證 API 實(shí)現(xiàn)器,即 hibernate )。使用休眠,您可以使用 maven 導(dǎo)入此依賴項(xiàng)
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.0.10.Final</version>
</dependency>
那么整個(gè)事情就變成了:
@RestController
@Validated
public class TestController {
@GetMapping("/test")
public void test(@RequestParam(value = "a", required = true ) @Size(min=1) String a,
@RequestParam(value = "b", required = true) @Size(min=1) String b) {
{
}
}
添加回答
舉報(bào)