java - 如何在实例化 Java Spring Webflux 时修改或更新 POJO 模型

标签 java spring spring-boot spring-webflux reactive-streams

我正在使用适用于 Java 的 Google map 地理编码 API。我有一个基本地址POJO。我想要做的是在lat属性上运行createLatCord方法,并使用地址、城市、州和邮政编码的正文响应 p>

我不知道我还应该在模型本身、 Controller 服务/存储库处进行修改?

我需要在创建时在 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

}

Controller :

@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);
        });
    }

最佳答案

如果我没猜错的话,你想在保存用户时更新纬度。由于您的服务已经收到 Mono,您可以对其进行 flatMap 来调用 google api,然后调用存储库。 它会是这样的:

@Override
public Mono<User> createUser(Mono<User> userMono) {
    return userMono.flatMap(user -> 
                            methodToCallGoogleApiAndSetValueToUser(user))
                   .subscribe(reactiveMongoOperations::save)
}

有一点是,methodToCallGoogleApiAndSetValueToUser 应该返回一个带有更新后的用户的 Mono。

希望这对您有所帮助!

关于java - 如何在实例化 Java Spring Webflux 时修改或更新 POJO 模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57469370/

相关文章:

Java、Spring 3.0.5 与 Apachetiles 2.2.2 集成

google-app-engine - Jetty 无法在 appengine flexible 中启动 spring boot 应用程序

spring - 用于 Spring Boot 测试的嵌入式 Postgres

java - 碰撞检测 - 防止玩家在撞击物体后移动

JavaFX 错误 : ANOMALY: meaningless REX prefix used

java - Get() 模式以避免 nullPointerException

java - 如何在java中给定时间间隔更新数据库中的值?

model-view-controller - Spring MVC - session - 请求

java - 使用 grapiql 的 GraphQl 变量 - 变量未定义

java - 带有配置文件的 Spring Boot Maven 多模块项目 - 未找到其他模块的包