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

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