简体   繁体   English

如何正确使用 Optional 和 `@RequestBody`?

[英]How can I use properly Optional with a `@RequestBody`?

I use the Spring Boot 2.0.8.RELEASE.我使用的是 Spring Boot 2.0.8.RELEASE。

I have a @RestController method looks like this:我有一个@RestController方法,如下所示:

    @PostMapping("/message")
    public PushMessageResponse sendPush(@Validated @RequestBody PushMessageRequest pushMessageRequest) {
        final List<String> sentMessage = pushService.sendMessage(pushMessageRequest);

        return new PushMessageResponse(sentMessage);
    }

And I have a PushMessageRequest class that looks like this:我有一个PushMessageRequest类,如下所示:

@RequiredArgsConstructor
@Data
public class PushMessageRequest {

    private final Optional<UUID> clientId;
    private final Optional<String> token;
    private final Optional<String> text;
    private final Optional<PushOptions> pushOptions;
    @NotBlank
    private final String appName;

}

And If I send a request to this method with a body that doesn't contain some property I see that that kind of property(those that I didn't pass in the JSON request) is null .如果我向这个方法发送一个请求,其主体不包含某些属性,我会看到那种属性(我没有在 JSON 请求中传递的那些)为null

I've already tried to add such configuration:我已经尝试添加这样的配置:

@Configuration
public class JacksonConfiguration {

    @Bean
    public Module jdk8Module() {
        return new Jdk8Module();
    }
}

I have expected to see that the Optional property of my DTO isn't null but such property has a value of Optional.empty() .我希望看到我的 DTO 的 Optional 属性不为空,但该属性的值为Optional.empty()

According to @cameron1024, @AmanGarg, @RavindraRanwala I shouldn't use Optional as a field, because it is not intended to.根据@cameron1024、@AmanGarg、@RavindraRanwala,我不应该使用 Optional 作为字段,因为它不是故意的。

I choose the solution of creating methods that return Optional#ofNullable() of the field.我选择创建返回字段的Optional#ofNullable()的方法的解决方案。

Why do you want to use Optional here.为什么要在这里使用Optional You can simply use regular classes, and use Optional.ofNullable() to create Optional objects inside your controller class.您可以简单地使用常规类,并使用Optional.ofNullable()在您的控制器类中创建Optional对象。

The Optional API was primarily intended to be used for methods that return a value that may be null, rather than transmitting data. Optional API 主要用于返回可能为 null 的值的方法,而不是传输数据。 For example, you can write your message class as:例如,您可以将消息类编写为:

public class PushMessageRequest {
    private UUID clientId;
    ...
}

and in your controller class, you can call:在您的控制器类中,您可以调用:

@PostMapping("/message")
public PushMessageResponse sendPush(@Validated @RequestBody PushMessageRequest pushMessageRequest) {
    Optional<UUID> uuidOptional = Optional.ofNullable(pushMessageRequest.getUUID());
    ...
}

For a more detailed explanation of why you should not use Optional in a class that is serialized, refer to: Why java.util.Optional is not Serializable, how to serialize the object with such fields .有关为什么不应在已序列化的类中使用Optional的更详细说明,请参阅: Why java.util.Optional is not Serializable, how to serialize the object with such fields Although this refers specifically to standard Java serialization, the reasoning applies to JSON serialization as well.虽然这特指标准 Java 序列化,但推理也适用于 JSON 序列化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM