将Java 8语法转换为匿名内部类

huangapple go评论71阅读模式
英文:

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

huangapple
  • 本文由 发表于 2020年8月28日 11:43:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/63627143.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定