我想为自定义约束验证器添加 2 个值,因为我有 2 个功能标志:
@JsonProperty(value = "name")
@BlockedWithoutEnabledFeatureFlag(feature = FeatureFlag.AAA, values = {"aaa", "bbb"})
@BlockedWithoutEnabledFeatureFlag(feature = FeatureFlag.BBB, values = {"ccc", "ddd"})
private String parameter;
Run Code Online (Sandbox Code Playgroud)
在哪里:
@Constraint(validatedBy = {BlockedWithoutEnabledFeatureFlagValidator.class})
@Target({FIELD, PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
@ReportAsSingleViolation
public @interface BlockedWithoutEnabledFeatureFlag {
String message() default "{validation.constraints.BlockedWithoutEnabledFeatureFlag.message}";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
FeatureFlag feature();
String[] values() default {};
}
Run Code Online (Sandbox Code Playgroud)
和实施:
public class BlockedWithoutEnabledFeatureFlagValidator implements ConstraintValidator<BlockedWithoutEnabledFeatureFlag, Object> {
private final FeatureFlagService featureFlagService;
private List<String> blocked;
private FeatureFlag feature;
@Override
public void initialize(BlockedWithoutEnabledFeatureFlag …Run Code Online (Sandbox Code Playgroud) 我想知道为什么我的组件SearchResults被渲染了两次。
在MainPage组件中,我想传递offers给子组件SearchResults:
const mainPage = () => {
const [offers, setOffers] = useState(null);
useEffect(() => {
onInitOffers();
}, [])
const onInitOffers = () => {
axios.get('/offers')
.then(response => {
setOffers(response.data);
})
.catch(error => {
console.log(error);
})
}
const searchResults = (
<SearchResults
searchedOffers={offers}
/>
);
return (
<Aux>
<div className={classes.container}>
<div className={classes.contentSection}>
{searchResults}
</div>
</div>
</Aux>
)
}
export default mainPage;
Run Code Online (Sandbox Code Playgroud)
为什么组件SearchResults被渲染两次?如何offers使用钩子正确传递给子组件?
在我的子组件 SearchResults 中,我必须添加 if 避免错误映射的条件不是函数:
const …Run Code Online (Sandbox Code Playgroud) 如何"2020-05-22T12:51:20.732111Z"在 Java 中解析为 Instant?
我用了:
LocalDateTime.parse(
startTime, DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.US))
.atZone(ZoneId.of("America/Toronto"))
.toInstant()
Run Code Online (Sandbox Code Playgroud)
但有错误:
Exception in thread "main" java.time.format.DateTimeParseException: Text '2020-05-22T12:51:20.732111Z' could not be parsed at index 24
at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1949)
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851)
at java.time.LocalDateTime.parse(LocalDateTime.java:492)
at Instant.Main.main(Main.java:54)
Run Code Online (Sandbox Code Playgroud) 我想从以下位置获取以下数据:
MyObject.builder()
.lastUpdated(tuple.getT2().isEmpty() ? null : tuple.getT2().get(0).getLastUpdated().toInstant())
...
...
.build()
Run Code Online (Sandbox Code Playgroud)
tuple.getT2().get(0).getLastUpdated() 可以为空...
我试过:
.lastUpdated(
tuple.getT2().stream()
.map(Optional::ofNullable)
.findFirst()
.flatMap(Function.identity())
.map(metadata -> metadata.getLastUpdated().toInstant()) //NPE
.orElse(null))
Run Code Online (Sandbox Code Playgroud)
但我得到了 NPE
.map(metadata -> metadata.getLastUpdated().toInstant())
Run Code Online (Sandbox Code Playgroud) 我想知道检查列表是否为空的最佳方法是什么。在我的直播中,我拨打了orElseThrow两次电话。它有效,但我不知道它是否正确?看起来有点难看:
Optional.ofNullable(listCanBeNull)
.orElseThrow(() -> new ResourceNotFoundException("the same error message"))
.stream()
.filter(configuration -> configuration.getId().equals(warehouseConfigurationId))
.findAny()
.orElseThrow(() -> new ResourceNotFoundException("the same error message"));
Run Code Online (Sandbox Code Playgroud)
当列表为空且未找到任何项目时,我必须抛出错误