java - 如何在实例化 Java Spring Webflux 时修改或更新 POJO 模型
问题描述
我正在使用适用于 Java 的 Google Maps Geocoding API。我有一个基本地址 POJO。我想要做的是在lat 属性上运行createLatCord方法,其中包含地址、城市、州和邮政编码的正文响应
我不知道我还应该在哪里修改,在模型本身,控制器或服务/存储库?
方法我需要在创建时在 lat 属性上运行
private double createLatCord() throws InterruptedException, ApiException, IOException {
GeoApiContext context = new
GeoApiContext.Builder().apiKey("abc").build();
GeocodingResult[] results = GeocodingApi.geocode(context, address+city+state+zipcode).await();
// Gson gson = new GsonBuilder().setPrettyPrinting().create();
return results[0].geometry.location.lat;
}
模型 :
// all the imports ...
public class User {
private String address;
private String zipcode;
private String city;
private String state;
private double lat; // <-- Run createLatCord method on this property
@Id
private String id;
public User() {
}
public User(String address, String zipcode, String city, String state, double lat) throws InterruptedException, ApiException, IOException {
this.address = address;
this.city = city;
this.state = state;
this.zipcode = zipcode;
this.lat = lat;
}
// GETTERS AND SETTERS HERE FOR THE ABOVE
// Leaving it out cause it's alot
}
控制器:
@PostMapping(path = "", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
public Mono<User> createUser(@RequestBody Mono<User> user) {
return userService.createUser(user);
}
服务/存储库:
@Override
public Mono<User> createUser(Mono<User> userMono) {
return reactiveMongoOperations.save(userMono);
}
解决方案:
模型:
public Mono<User> createLatCord(User user) throws InterruptedException, ApiException, IOException {
GeoApiContext context = new GeoApiContext.Builder().apiKey("elnjvw").build();
GeocodingResult[] results = GeocodingApi.geocode(context, user.getAddress() + user.getCity() + user.getState() + user.getZipcode()).await();
user.setLat(results[0].geometry.location.lat);
user.setLng(results[0].geometry.location.lng);
return Mono.just(user);
}
服务/存储库
@Override
public Mono<User> createUser(Mono<User> userMono) {
return userMono.flatMap(user -> {
try {
return reactiveMongoOperations.save(
user.createLatCord(user));
} catch (InterruptedException | ApiException | IOException e) {
e.printStackTrace();
}
return Mono.just(user);
});
}
解决方案
如果我做对了,您想在保存用户时更新 lat。由于您的服务已经收到 Mono,您可以将其 flatMap 以调用 google api,然后调用存储库。它会是这样的:
@Override
public Mono<User> createUser(Mono<User> userMono) {
return userMono.flatMap(user ->
methodToCallGoogleApiAndSetValueToUser(user))
.subscribe(reactiveMongoOperations::save)
}
一点是methodToCallGoogleApiAndSetValueToUser应该返回一个带有更新用户的 Mono。
希望这可能会有所帮助!
推荐阅读
- javascript - 尝试使用 JavaScript 客户端在不和谐中发送一个命令时出现不和谐 API 错误
- python - 在 sklearn 中将 DataFrameMapper() 用于 PolynomialFeature() 时出错
- c - 调用用户定义的函数后出现“双精度前的预期表达式”错误
- java - java.io.IOException:无法运行程序执行失败的android
- mysql - 如何查询具有相同字段的两个表,仅当字段值相同时才返回id
- nativescript - 在 NativeScript 中关闭 HTTP 请求
- reactjs - 'requireNativeComponent' 不是从 'react-native-web/dist/index 导出的
- android - Android Studio 4.0.1 发布/调试构建类型导致 R8/Proguard 不可行/可行的结果
- python - 通过 for 循环返回函数的值在多次迭代中不起作用
- hibernate - 像 ehcache / redis 这样的外部缓存在哪里适合 wrt hibernate level 1 / level 2 缓存?