java - 调用端点时如何在 MongoDB 中填充表?- MongoDb,SpringBoot
问题描述
我正在将 Spring Boot 与 MongoDB 一起使用。我是 MongoDb 的初学者。
我想要实现的是,当我在我的端点上接到电话时,或者当我的端点被请求时,我想在数据库中创建一个表,该表将具有 status REQUESTED
,applyed userID
,然后是 fields url
,version
,requestedDate
(在创建表时填充),completedDate
(将 URL 保存到数据库时填充)。
之后,我执行所有必须执行的逻辑,在我的案例中,逻辑是我应该检索用户的 URL 并将其保存到发出 URL 请求时创建的同一个表中。
在 URL 保存在请求时创建的同一个表中后,我应该有, userId
, url
,version
和state as 。requestedDate
completedDate
COMPLETED
我到目前为止的代码是:
用户网址.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
构造函数,那么我想在创建时也有它,但是我应该对它应用什么值?
任何建议表示赞赏:)
解决方案
您应该使用使用 持久化的实体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
);
}
}
推荐阅读
- javascript - 非常令人困惑的离子错误 - 2 个声明或没有
- azure - Azure 事件网格 - Azure Function 的事件传递安全性
- node.js - 在 botframework sdk nodejs 中创建外部链接
- sql - 表值参数游标顺序
- html - 使用 beautifulsoup 从网络抓取中获取错误的文本
- python - 如何根据带有条件的列值在数据框中插入行?
- python - Python 导入 pyLDAvis 模块 - 弃用警告
- asp.net - ASP.NET Web 表单应用程序文件夹未部署到 AWS Elastic Beanstalk 应用程序
- c++ - 带有 g++ 的 SDL:stdint.h:没有这样的文件或目录
- python - Python 导入名称冲突