亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何為帶請求參數和不帶請求參數的請求定義不同的 Spring MVC 請求處理程序?

如何為帶請求參數和不帶請求參數的請求定義不同的 Spring MVC 請求處理程序?

天涯盡頭無女友 2023-03-31 17:05:58
Spring MVC 有沒有辦法為沒有請求參數的請求和有請求參數的請求定義不同的處理程序?有一個簡單的控制器:@RestController@RequestMapping("/strategies")public class StrategyController {    ...    @GetMapping    public List<Strategy> getAll() {        return service.getBeans().stream()            .map(mapper::toDto)            .collect(toList());    }    @GetMapping    public List<Strategy> search(StrategyFilter filter) {        return service.search(new StrategySearchSpecification(                filter.getCode(),                filter.getName(),                filter.getType()            )).stream()            .map(mapper::toDto)            .collect(toList());    }}我想要getAll()方法來處理沒有請求參數的請求: /strategies我想要search(StrategyFilter filter)方法來處理帶有請求參數的請求: /strategies?name=SomeName&type=SomeType似乎無法通過params屬性來區分這種情況,因為可以省略@GetMapping任何屬性。StrategyFilter在此配置中,我得到一個明顯的錯誤:Caused by: java.lang.IllegalStateException: Ambiguous mapping. Cannot map 'strategyController' method public List<Strategy> StrategyController.getAll() to {[/strategies],methods=[GET]}: There is already 'strategyController' bean method public List<Strategy> StrategyController.search(StrategyFilter) mapped.當然也可以這樣寫:@GetMappingpublic List<Strategy> get(StrategyFilter filter) {    return noFilterProvided(filter) ? getAll() : search(filter);}但是每次過濾器的屬性數量發生變化時,都需要更改“noFilterProvided(StrategyFilter filter)”。
查看完整描述

2 回答

?
翻過高山走不出你

TA貢獻1875條經驗 獲得超3個贊

Spring 框架使用基于點的匹配。它從可用的匹配中選擇最高的匹配。通過標準越多的人得分越高。如果您在一個匹配中定義了請求的查詢參數,那么當參數存在時它將被接受。其他情況另說。


要定義請求的參數,請將它們作為直接屬性傳遞,而不是作為 StrategyFilter 屬性傳遞。在缺少參數的情況下,這樣的實例初始化也成功(這些屬性不會被初始化,它們保持默認狀態:“”/0/false)。所以會出現模棱兩可的匹配錯誤。


最后:使用直接屬性而不是 StrategyFilter。


您設計的其他問題是直接 StrategySearchSpecification 實例化。它不是以這種方式進行單元測試的。將其定義為 Spring 組件。


@Component

@Getter // Lombok annotation to generate getter methods

@Setter // Lombok annotation to generate setter methods

public class StrategySearchSpecification

{

  private CODE_TYPE code;

  private String name;

  private TYPE_TYPE type;

}

并將其作為參數注入(正確的實現/模擬)并使用它的設置方法。


@RestController

@RequestMapping("/strategies")

public class StrategyController {

    ...


    @GetMapping

    public List<Strategy> getAll() {

        return service.getBeans().stream()

            .map(mapper::toDto)

            .collect(toList());

    }


    @GetMapping

    public List<Strategy> search(@RequestParam CODE_TYPE code, @RequestParam String name, @RequestParam TYPE_TYPE type, StrategySearchSpecification specification ) {

        specification.setCode( code );

        specification.setName( name );

        specification.setType( type );

        return service.search( specification

            )).stream()

            .map(mapper::toDto)

            .collect(toList());

    }

}


查看完整回答
反對 回復 2023-03-31
?
心有法竹

TA貢獻1866條經驗 獲得超5個贊

如果StrategyFilter具有屬性nameand type,這應該有效:


@RestController

@RequestMapping("/strategies")

public class StrategyController {

    ...


    @GetMapping

    public List<Strategy> getAll() {

        return service.getBeans().stream()

            .map(mapper::toDto)

            .collect(toList());

    }


    @GetMapping("{name}/{type}")

    public List<Strategy> search(StrategyFilter filter) {

        return service.search(new StrategySearchSpecification(

                filter.getCode(),

                filter.getName(),

                filter.getType()

            )).stream()

            .map(mapper::toDto)

            .collect(toList());

    }

}


查看完整回答
反對 回復 2023-03-31
  • 2 回答
  • 0 關注
  • 136 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號