英文:
Converting Java 8 Syntax to Anonymous Inner Class
问题
webclient.get().uri(new UriBuilder() {
@Override
public URI build(Object... values) {
UriBuilder uriBuilder = new DefaultUriBuilderFactory().uriString("");
uriBuilder.path("/api/person/{personId}")
.queryParam("param1", aDouble)
.queryParam("param2", "A string value with spaces")
.queryParam("param3", aListOfValues)
.queryParam("param4", null);
return uriBuilder.build(anInteger);
}
});
英文:
How can I convert uriBuilder Lambda function into an anonymous inner class?
webclient.get().uri(uriBuilder -> uriBuilder.path("/api/person/{personId}")
.queryParam("param1", aDouble)
.queryParam("param2", "A string value with spaces")
.queryParam("param3", aListOfValues)
.queryParam("param4", null)
.build(anInteger))
答案1
得分: 1
假设webclient
是一个 org.springframework.web.reactive.function.client.WebClient
,那么这个方法是 uri(Function<UriBuilder,URI> uriFunction)
,这意味着您需要一个实现函数式接口 Function<UriBuilder,URI>
的匿名类:
new Function<UriBuilder,URI>() {
@Override
public URI apply(UriBuilder uriBuilder) {
return uriBuilder.path("/api/person/{personId}")
.queryParam("param1", aDouble)
.queryParam("param2", "A string value with spaces")
.queryParam("param3", aListOfValues)
.queryParam("param4", null)
.build(anInteger);
}
}
英文:
Assuming webclient
is a org.springframework.web.reactive.function.client.WebClient
, then the method is uri(Function<UriBuilder,URI> uriFunction)
, which means you need an anonymous class implementing functional interface Function<UriBuilder,URI>
:
new Function<UriBuilder,URI>() {
@Override
public URI apply(UriBuilder uriBuilder) {
return uriBuilder.path("/api/person/{personId}")
.queryParam("param1", aDouble)
.queryParam("param2", "A string value with spaces")
.queryParam("param3", aListOfValues)
.queryParam("param4", null)
.build(anInteger);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论