第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

是否可以有條件地在@RequestParam 中分配Required 的值?

是否可以有條件地在@RequestParam 中分配Required 的值?

慕娘9325324 2022-06-04 10:59:08
我的控制器中有 2 個(gè) @RequestParam 參數(shù)。我想根據(jù)條件設(shè)置兩個(gè)參數(shù)的必需值。條件可能是,如果傳遞了一個(gè)參數(shù),則必須傳遞另一個(gè)參數(shù)。所以將 other 的 required 設(shè)置為 true ,反之亦然。否則,如果沒有傳遞任何參數(shù),則將兩者都設(shè)置為 false。@RestControllerpublic class TestController {@GetMapping("/test")public void test(@RequestParam(value = "a", required = (b !=null) String a, @RequestParam(value = "b", required = (a !=null) )  String b,) {     {     }}在@RequestParam() 中使用變量名的語法是錯(cuò)誤的,但我想解釋一下我想要什么。
查看完整描述

3 回答

?
阿波羅的戰(zhàn)車

TA貢獻(xiàn)1862條經(jīng)驗(yàn) 獲得超6個(gè)贊

您可以使用以下 2 種方式之一來執(zhí)行此操作:

  1. 使用 Spring AOP 并為該請求映射創(chuàng)建一個(gè)環(huán)繞方面

  2. 使用 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或任何其他對您的情況更有意義的東西。


查看完整回答
反對 回復(fù) 2022-06-04
?
holdtom

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

}

我希望這可以滿足您的要求。


查看完整回答
反對 回復(fù) 2022-06-04
?
素胚勾勒不出你

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) {

     {


     }


}


查看完整回答
反對 回復(fù) 2022-06-04
  • 3 回答
  • 0 關(guān)注
  • 174 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)