小编Mat*_*ley的帖子

具有多个值的 Spring Boot 自定义约束验证器

我想为自定义约束验证器添加 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)

java spring-boot

2
推荐指数
1
解决办法
271
查看次数

React useState hook - 组件被渲染两次

我想知道为什么我的组件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)

reactjs react-hooks

1
推荐指数
1
解决办法
2140
查看次数

在 Java 中将日期“2020-05-22T12:51:20.765111Z”解析为 Instant

如何"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)

java datetime

0
推荐指数
1
解决办法
267
查看次数

Java 8 流 - 避免 NPE

我想从以下位置获取以下数据:

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)

java optional java-stream

0
推荐指数
1
解决办法
63
查看次数

Java Streams - 检查列表是否为空

我想知道检查列表是否为空的最佳方法是什么。在我的直播中,我拨打了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)

当列表为空且未找到任何项目时,我必须抛出错误

java java-stream

0
推荐指数
1
解决办法
1767
查看次数