亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在實例化 Java Spring Webflux 時修改或更新 POJO 模型

如何在實例化 Java Spring Webflux 時修改或更新 POJO 模型

互換的青春 2023-06-04 15:38:42
我正在使用適用于 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。

希望這可能會有所幫助!


查看完整回答
反對 回復 2023-06-04
  • 1 回答
  • 0 關注
  • 148 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號