我正在使用適用于 Java 的 Google Maps Geocoding API。我有一個基本地址 POJO。我想要做的是在lat 屬性上運行createLatCord方法,主體響應是地址、城市、州和郵政編碼我不知道我還應該在哪里修改,在模型本身,Controller或Service/Repository處?方法我需要在創建時在 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);}服務/存儲庫:@Overridepublic Mono<User> createUser(Mono<User> userMono) { return reactiveMongoOperations.save(userMono);}
1 回答

慕姐4208626
TA貢獻1852條經驗 獲得超7個贊
如果我做對了,你想在保存用戶時更新緯度。由于您的服務已經收到一個 Mono,您可以對其進行平面映射以調用 google api,然后調用存儲庫。它會是這樣的:
@Override public Mono<User> createUser(Mono<User> userMono) { return userMono.flatMap(user -> methodToCallGoogleApiAndSetValueToUser(user)) .subscribe(reactiveMongoOperations::save) }
有一點是methodToCallGoogleApiAndSetValueToUser應該返回一個帶有更新用戶的 Mono。
希望這可能會有所幫助!
添加回答
舉報
0/150
提交
取消