在 Dropwizard 中正确使用 NonEmptyStringParam



我是使用Dropwizard的新手,我正在按照网站上的教程创建hello world应用程序。谁能向我解释如何使用NonEmptyStringParam打印类似"Hello,陌生人!"的东西,如果没有提供参数来说Hello?

以下是我的资源代码及其输出:

{"id":1,"content":"Hello, Optional[Stranger]!"}

而不是

{"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);
}
}

谢谢!

你不应该用Optional<>包裹NonEmptyStringParam。请参阅来自 Dropwizard 源代码的测试用例。 还要在构造函数方法中删除带有NonEmptyStringParamdefaultName包装。

相关内容

  • 没有找到相关文章

最新更新