我不理解我在这里的文档中看到的这个Java构造.
我认为我理解但似乎无法复制的部分是这个例子:
public static <W> HttpResponse.BodySubscriber<Supplier<W>> asJSON(Class<W> targetType) {
HttpResponse.BodySubscriber<InputStream> upstream = HttpResponse.BodySubscribers.ofInputStream();
HttpResponse.BodySubscriber<Supplier<W>> downstream = HttpResponse.BodySubscribers.mapping(
upstream,
(InputStream is) -> () -> {
try (InputStream stream = is) {
ObjectMapper objectMapper = new ObjectMapper();
return objectMapper.readValue(stream, targetType);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
);
return downstream;
}
具体地说,(InputStream is) -> () -> {
部分.当我读到它的时候,我想它应该是Supplier
,提供它所做的事情的Function
.然而,当我自己try 编写这样的东西时,它不起作用.
public static void main(String[] args) throws IOException {
String r = myMethod((String s)->()->{return s + "ddd";});
}
public static String myMethod(Function<String, String> func) {
return func.apply("Hello");
}
给我的编译错误是Target type of a lambda conversion must be an interface
.但是,Function
是一个接口.
我有什么不明白的?