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());
}
}

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());
}
}
添加回答
舉報