java - Spring - 返回端点的字符串结果并将其存储在变量中
问题描述
这是我的端点,登录后它会返回我的电子邮件地址
@GetMapping("/user")
public String userInfo(Authentication authentication) {
String userName = authentication.getName();
return userName;
}
我想给createdBy
变量这个值
@PostMapping("/request")
public RequestDTO saveRequest(@RequestBody final RequestDTO requestDTO) {
requestDTO.setCreatedBy(InsertTheNewStringVariableHere);
return requestUseCase.createRequest(requestDTO);
}
解决方案
这一切都取决于您如何访问您的rest api
.
如果你拥有代码,我的意思是如果你的类在同一个项目中,你可以这样做:
@PostMapping("/request")
public RequestDTO saveRequest(@RequestBody final RequestDTO requestDTO) {
requestDTO.setCreatedBy(yourServiceInstance.userInfo(authentication));
return requestUseCase.createRequest(requestDTO);
}
如果它是 external api
,您将不得不从 a 调用该服务http client
:
@PostMapping("/request")
public RequestDTO saveRequest(@RequestBody final RequestDTO requestDTO) {
requestDTO.setCreatedBy(yourHttpClient.callUserInfo(authentication));
return requestUseCase.createRequest(requestDTO);
}
你将在你的类中注入你的userInfo
对象和一个Authentication
对象。
推荐阅读
- sql-server - 文本文件到 SQL
- android - 通过 Glide 在线性布局中加载 GIF 作为背景
- java - 从服务器更新 java 代码
- javascript - 从数组中获取具有最低值属性的对象
- javascript - 当 .toFixed(2) 小数点后为零时,如何将浮点数格式化为整数?
- python - 异步服务调用 Python
- sql - 在 mod(rownum,10) = number 上更新表的所有行中的列值
- .net - 条带订阅 webhook
- android - com.android.dex.DexException: 多个 dex 文件定义 Lorg/apache/cordova/Config
- sqlite - 在 SQLite 中减去 COUNT 结果