Reputation: 31
I'm a newbee using Dropwizard and I'm following the tutorial in the website to create the hello world application. Can anyone please explain to me how to use NonEmptyStringParam to print something like "Hello, stranger!" if no parameter is provided to sayHello?
The following is my Resource code and it outputs:
{"id":1,"content":"Hello, Optional[Stranger]!"}
instead of
{"id":1,"content":"Hello, Stranger!"}
public class HelloWorldResource {
private final String template;
private final NonEmptyStringParam defaultName;
private final AtomicLong counter;
public HelloWorldResource(String template, String defaultName) {
this.template = template;
this.defaultName = new NonEmptyStringParam(defaultName);
this.counter = new AtomicLong();
}
@GET
@Timed
public Saying sayHello(@QueryParam("name") Optional<NonEmptyStringParam> name) {
final String value = String.format(template, name.orElse(defaultName));
return new Saying(counter.incrementAndGet(), value);
}
}
Thanks!
Upvotes: 1
Views: 198
Reputation: 1157
You shouldn't wrap the NonEmptyStringParam
with Optional<>
. See the testcase from the Dropwizard source.
Also remove the wrap of defaultName
with NonEmptyStringParam
in the constructor method.
Upvotes: 1