首页 > 解决方案 > 调用端点时如何在 MongoDB 中填充表?- MongoDb,SpringBoot

问题描述

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

我想要实现的是,当我在我的端点上接到电话时,或者当我的端点被请求时,我想在数据库中创建一个表,该表将具有 status REQUESTED,applyed userID,然后是 fields urlversionrequestedDate(在创建表时填充),completedDate(将 URL 保存到数据库时填充)。

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

在 URL 保存在请求时创建的同一个表中后,我应该有, userId, url,version和state as 。requestedDatecompletedDateCOMPLETED

我到目前为止的代码是:

用户网址.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
            );
        }
}

用户控制器.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);
        }
    }

我不确定我应该如何填充UserUrl模型URL

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

任何建议表示赞赏:)

标签: javamongodbspring-boot

解决方案


您应该使用使用 持久化的实体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并将 初始化completeDateInstant.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
        );
    }
}

推荐阅读