quarkus-for-spring-developers / examples

Example code for the Quarkus for Spring Developers eBook
https://red.ht/quarkus-spring-devs
Apache License 2.0
46 stars 17 forks source link

Chapter 6: quarkus-rest-cloud-config: Greeting property injection? #78

Closed edeandrea closed 3 years ago

edeandrea commented 3 years ago

Sub-issue of quarkus-for-spring-developers/project-management#32

In GreeterResource.java, rather than injecting 3 separate properties via @ConfigProperty, would it instead be better to group the properties in a single configuration class? I showcase pretty much this same example configuration class in Chapter 2.

@ConfigMapping(prefix = "greeting")
public interface GreetingProperties {
  String message();

  @WithDefault("!")
  String suffix();

  Optional<String> name();
}

then refactor the GreeterResource.java

@Path("/hello")
public class GreeterResource {
  private final GreetingProperties greetingProperties;

  public GreeterResource(GreetingProperties greetingProperties) {
    this.greetingProperties = greetingProperties;
  }

  @GET
  @Produces(MediaType.TEXT_PLAIN)
  public String hello() {
    return greetingProperties.message() + " " + greetingProperties.name().orElse("world") + greetingProperties.suffix();
  }
}
edeandrea commented 3 years ago

@cmoulliard thoughts?