【问题标题】:How to populate table in MongoDB when call to endpoint is made? - MongoDb, SpringBoot调用端点时如何在 MongoDB 中填充表? - MongoDb,Spring Boot
【发布时间】:2021-11-06 11:27:22
【问题描述】:

我正在将 Spring Boot 与 MongoDB 一起使用。我是 MongoDb 的初学者。

我想要实现的是当我在我的端点上接到电话时,或者当我的端点被请求时,我想在数据库中创建一个表,状态为REQUESTED,应用userID,然后是字段urlversionrequestedDate(在创建表时填充)、completedDate(在将 URL 保存到数据库时填充)。

之后,我执行所有我必须做的逻辑,在我的情况下,我应该检索用户的 URL 并将其保存到发出 URL 请求时创建的同一个表中。

将 URL 保存在请求时创建的同一个表中后,我应该有 userIdurlversionrequestedDatecompletedDate 并声明为 COMPLETED

到目前为止我的代码是:

UserUrl.java

public class UserUrl{
        public final MongoUserId mongoUserId;
        public final Optional<String> url;
        public final long version;
        public final Instant requestedDate;
        public final Instant completeDate;
        public final State state;
    
    
        public UserUrl(MongoUserId mongoUserId,
                       Optional<String> url,
                       long version,
                       Instant requestedDate,
                       State state) {
            this.mongoUserId = mongoUserId;
            this.url = url;
            this.version = version;
            this.requestedDate = requestedDate;
            this.state = state;
        }
    
        public static UserUrl create(MongoUserId mongoUserId){
            return new UserUrl(
                    mongoUserId,
                    Optional.empty(),
                    0,
                    Instant.now(),
                    State.REQUESTED
                    );
        }
    
        public UserUrl withUrl(String url){
            return new UserUrl(
                    this.mongoUserId,
                    Optional.of(url),
                    0,
                    Instant.now(),
                    State.COMPLETED
            );
        }
}

UserController.java

@RequestMapping(value = "/trigger/url/{userId}",
            method = RequestMethod.GET)
    public void getUrlForUser(@PathVariable("userId") String userId) {

        userUrlRepository.save(UserUrl.create(MongoUserId.of(userId)));

         URL getUrl = userDataService.getUrlUser(userId);
         String url = getUrl.toString();
         if (url != null) {
             
             //here I should probably do something to set withUrl method in model 

             return new ResponseEntity<>(url, HttpStatus.OK);
         } else {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
    }

当我有URL 时,我不确定应该如何填充UserUrl 模型?

另外,如果我把completedDate 放在构造函数中,那么我想在创建时也有它,但是我应该对它应用什么值?

任何建议表示赞赏:)

【问题讨论】:

    标签: java mongodb spring-boot


    【解决方案1】:

    您应该使用使用CrudRepository 持久化的实体,更新它并在需要时再次保存:

    @RequestMapping(value = "/trigger/url/{userId}", method = RequestMethod.GET)
    public void getUrlForUser(@PathVariable("userId") String userId) {
    
        UserUrl userUrl = userUrlRepository.save(UserUrl.create(MongoUserId.of(userId)));
    
        URL getUrl = userDataService.getUrlUser(userId);
        String url = getUrl.toString();
        if (url != null) {
            userUrlRepository.save(userUrl.withUrl(url));
            return new ResponseEntity<>(url, HttpStatus.OK);
        } else {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
    }
    

    编辑

    要在完成后更改completeDate,即当url 属性已解析时,请更新withUrl 构建器方法以传播this.requestedDate 并将completeDate 初始化为Instant.now()

    public class UserUrl {
    
        public final MongoUserId mongoUserId;
        public final Optional<String> url;
        public final long version;
        public final Instant requestedDate;
        public final Instant completeDate;
        public final State state;
    
    
        public UserUrl(MongoUserId mongoUserId,
                       Optional<String> url,
                       long version,
                       Instant requestedDate,
                       Instant completeDate,
                       State state) {
            this.mongoUserId = mongoUserId;
            this.url = url;
            this.version = version;
            this.requestedDate = requestedDate;
            this.completeDate = completeDate;
            this.state = state;
        }
    
        public static UserUrl create(MongoUserId mongoUserId){
            return new UserUrl(
                    mongoUserId,
                    Optional.empty(),
                    0,
                    Instant.now(),
                    null,
                    State.REQUESTED
                    );
        }
    
        public UserUrl withUrl(String url){
            return new UserUrl(
                    this.mongoUserId,
                    Optional.of(url),
                    0,
                    this.requestedDate,
                    Instant.now(),
                    State.COMPLETED
            );
        }
    }
    

    【讨论】:

    • 感谢您的回答。但是如何设置requestDatecompleteDate?我了解当我使用 Instant.now() 时,它将处于创建或更新状态。但是,就我而言,在使用 URL 更新表格后,我想设置 completeDate 并保持 requestDate 不变。
    • completeDate 声明在哪里?我在你的模型中看不到它。
    • 它应该在那里,但我不知道如何使用它:/我已经更新了我的问题。我不知道它应该是 Instant 还是其他?
    • 我已经更新了我的答案。我希望这会有所帮助。
    • 谢谢!这正是我想要的。
    猜你喜欢
    • 2017-09-06
    • 2020-05-26
    • 2018-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多