下面的示例应通过将JSON主体转换为POJO并使用.map()函数对其进行反应性处理。但是,我的控制器应用程序出现错误。谷歌搜索错误没有任何帮助。
这是来自micronaut文档(“使用Reactive Framework的绑定”)中显示的示例:
https://docs.micronaut.io/latest/guide/index.html#httpServer
@Controller("/people")
public class PersonController {
Map<String, Person> inMemoryDatastore = new LinkedHashMap<>();
@Post
public Single<HttpResponse<Person>> save(@Body Single<Person> person) {
return person.map(p -> {
inMemoryDatastore.put(p.getFirstName(), p);
return HttpResponse.created(p);
}
);
}
}
我的代码:
package movie.list.service;
import com.mongodb.BasicDBObject;
import com.mongodb.client.FindIterable;
import io.micronaut.http.HttpResponse;
import io.micronaut.http.annotation.*;
import io.reactivex.Single;
import org.bson.Document;
import javax.inject.Inject;
import java.util.List;
import java.util.stream.Collectors;
@Controller("/api")
public class ListController {
@Inject
private DbConn dbConn;
@Post("/user")
public Single<HttpResponse<User>> postUser(@Body Single<User> user) {
return user.map(u -> {
Document doc = new Document("userName", u.getUserName());
if(u.getMovies() != null) {
List<BasicDBObject> moviesDocList = u.getMovies().stream().map(m -> {
BasicDBObject dbMovie = new BasicDBObject();
dbMovie.append("imdbId", m.getImdbId());
dbMovie.append("inCollection", m.isInCollection());
return dbMovie;
}).collect(Collectors.toList());
doc.append("movies", moviesDocList);
}
dbConn.getCollection().insertOne(doc);
return HttpResponse.created(u);
});
}
}
IntelliJ控制台日志:
/Library/Java/JavaVirtualMachines/jdk1.8.0_201.jdk/Contents/Home/bin/java "-javaagent:/Applications/IntelliJ IDEA CE.app/Contents/lib/idea_rt.jar=52858:/Applications/IntelliJ IDEA CE.app/Contents/bin" -Dfile.encoding=UTF-8 -classpath /Library/Java/JavaVirtualMachines/jdk1.8.0_201.jdk/Contents/Home/jre/lib/charsets.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_201.jdk/Contents/Home/jre/lib/deploy.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_201.jdk/Contents/Home/jre/lib/ext/cldrdata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_201.jdk/Contents/Home/jre/lib/ext/dnsns.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_201.jdk/Contents/Home/jre/lib/ext/jaccess.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_201.jdk/Contents/Home/jre/lib/ext/jfxrt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_201.jdk/Contents/Home/jre/lib/ext/localedata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_201.jdk/Contents/Home/jre/lib/ext/nashorn.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_201.jdk/Contents/Home/jre/lib/ext/sunec.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_201.jdk/Contents/Home/jre/lib/ext/sunjce_provider.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_201.jdk/Contents/Home/jre/lib/ext/sunpkcs11.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_201.jdk/Contents/Home/jre/lib/ext/zipfs.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_201.jdk/Contents/Home/jre/lib/javaws.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_201.jdk/Contents/Home/jre/lib/jce.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_201.jdk/Contents/Home/jre/lib/jfr.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_201.jdk/Contents/Home/jre/lib/jfxswt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_201.jdk/Contents/Home/jre/lib/jsse.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_201.jdk/Contents/Home/jre/lib/management-agent.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_201.jdk/Contents/Home/jre/lib/plugin.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_201.jdk/Contents/Home/jre/lib/resources.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_201.jdk/Contents/Home/jre/lib/rt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_201.jdk/Contents/Home/lib/ant-javafx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_201.jdk/Contents/Home/lib/dt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_201.jdk/Contents/Home/lib/javafx-mx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_201.jdk/Contents/Home/lib/jconsole.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_201.jdk/Contents/Home/lib/packager.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_201.jdk/Contents/Home/lib/sa-jdi.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_201.jdk/Contents/Home/lib/tools.jar:/Users/edwinrietmeijer/Documents/Java/micronaut-demo/movie-list-service/target/classes:/Users/edwinrietmeijer/.m2/repository/io/micronaut/micronaut-discovery-client/1.0.3/micronaut-discovery-client-1.0.3.jar:/Users/edwinrietmeijer/.m2/repository/org/slf4j/slf4j-api/1.7.25/slf4j-api-1.7.25.jar:/Users/edwinrietmeijer/.m2/repository/io/micronaut/micronaut-http-client/1.0.3/micronaut-http-client-1.0.3.jar:/Users/edwinrietmeijer/.m2/repository/io/reactivex/rxjava2/rxjava/2.2.2/rxjava-2.2.2.jar:/Users/edwinrietmeijer/.m2/repository/org/reactivestreams/reactive-streams/1.0.1/reactive-streams-1.0.1.jar:/Users/edwinrietmeijer/.m2/repository/io/micronaut/micronaut-http-netty/1.0.3/micronaut-http-netty-1.0.3.jar:/Users/edwinrietmeijer/.m2/repository/io/micronaut/micronaut-buffer-netty/1.0.3/micronaut-buffer-netty-1.0.3.jar:/Users/edwinrietmeijer/.m2/repository/io/netty/netty-buffer/4.1.30.Final/netty-buffer-4.1.30.Final.jar:/Users/edwinrietmeijer/.m2/repository/io/netty/netty-common/4.1.30.Final/netty-common-4.1.30.Final.jar:/Users/edwinrietmeijer/.m2/repository/io/netty/netty-codec-http/4.1.30.Final/netty-codec-http-4.1.30.Final.jar:/Users/edwinrietmeijer/.m2/repository/io/netty/netty-codec/4.1.30.Final/netty-codec-4.1.30.Final.jar:/Users/edwinrietmeijer/.m2/repository/io/netty/netty-handler/4.1.30.Final/netty-handler-4.1.30.Final.jar:/Users/edwinrietmeijer/.m2/repository/io/netty/netty-handler-proxy/4.1.30.Final/netty-handler-proxy-4.1.30.Final.jar:/Users/edwinrietmeijer/.m2/repository/io/netty/netty-transport/4.1.30.Final/netty-transport-4.1.30.Final.jar:/Users/edwinrietmeijer/.m2/repository/io/netty/netty-resolver/4.1.30.Final/netty-resolver-4.1.30.Final.jar:/Users/edwinrietmeijer/.m2/repository/io/netty/netty-codec-socks/4.1.30.Final/netty-codec-socks-4.1.30.Final.jar:/Users/edwinrietmeijer/.m2/repository/io/micronaut/micronaut-websocket/1.0.3/micronaut-websocket-1.0.3.jar:/Users/edwinrietmeijer/.m2/repository/io/micronaut/micronaut-http-server-netty/1.0.3/micronaut-http-server-netty-1.0.3.jar:/Users/edwinrietmeijer/.m2/repository/io/micronaut/micronaut-http-server/1.0.3/micronaut-http-server-1.0.3.jar:/Users/edwinrietmeijer/.m2/repository/io/micronaut/micronaut-router/1.0.3/micronaut-router-1.0.3.jar:/Users/edwinrietmeijer/.m2/repository/io/micronaut/micronaut-inject/1.0.3/micronaut-inject-1.0.3.jar:/Users/edwinrietmeijer/.m2/repository/javax/inject/javax.inject/1/javax.inject-1.jar:/Users/edwinrietmeijer/.m2/repository/io/micronaut/micronaut-core/1.0.3/micronaut-core-1.0.3.jar:/Users/edwinrietmeijer/.m2/repository/com/google/code/findbugs/jsr305/3.0.2/jsr305-3.0.2.jar:/Users/edwinrietmeijer/.m2/repository/org/yaml/snakeyaml/1.23/snakeyaml-1.23.jar:/Users/edwinrietmeijer/.m2/repository/javax/annotation/javax.annotation-api/1.3.2/javax.annotation-api-1.3.2.jar:/Users/edwinrietmeijer/.m2/repository/io/micronaut/micronaut-validation/1.0.3/micronaut-validation-1.0.3.jar:/Users/edwinrietmeijer/.m2/repository/io/micronaut/micronaut-http/1.0.3/micronaut-http-1.0.3.jar:/Users/edwinrietmeijer/.m2/repository/javax/validation/validation-api/2.0.1.Final/validation-api-2.0.1.Final.jar:/Users/edwinrietmeijer/.m2/repository/io/micronaut/micronaut-runtime/1.0.3/micronaut-runtime-1.0.3.jar:/Users/edwinrietmeijer/.m2/repository/io/micronaut/micronaut-aop/1.0.3/micronaut-aop-1.0.3.jar:/Users/edwinrietmeijer/.m2/repository/com/fasterxml/jackson/core/jackson-databind/2.9.7/jackson-databind-2.9.7.jar:/Users/edwinrietmeijer/.m2/repository/com/fasterxml/jackson/core/jackson-annotations/2.9.7/jackson-annotations-2.9.7.jar:/Users/edwinrietmeijer/.m2/repository/com/fasterxml/jackson/core/jackson-core/2.9.7/jackson-core-2.9.7.jar:/Users/edwinrietmeijer/.m2/repository/com/fasterxml/jackson/datatype/jackson-datatype-jdk8/2.9.7/jackson-datatype-jdk8-2.9.7.jar:/Users/edwinrietmeijer/.m2/repository/com/fasterxml/jackson/datatype/jackson-datatype-jsr310/2.9.7/jackson-datatype-jsr310-2.9.7.jar:/Users/edwinrietmeijer/.m2/repository/ch/qos/logback/logback-classic/1.2.3/logback-classic-1.2.3.jar:/Users/edwinrietmeijer/.m2/repository/ch/qos/logback/logback-core/1.2.3/logback-core-1.2.3.jar:/Users/edwinrietmeijer/.m2/repository/org/mongodb/mongo-java-driver/3.9.1/mongo-java-driver-3.9.1.jar:/Users/edwinrietmeijer/.m2/repository/com/google/code/gson/gson/2.8.5/gson-2.8.5.jar movie.list.service.Application
14:20:58.019 [main] DEBUG i.m.h.server.netty.NettyHttpServer - Binding movie-list-service server to *:8082
14:21:03.328 [main] INFO io.micronaut.runtime.Micronaut - Startup completed in 6370ms. Server Running: http://localhost:8082
14:21:03.448 [nioEventLoopGroup-1-3] INFO i.m.d.registration.AutoRegistration - Registered service [movie-list-service] with Consul
14:21:10.877 [nioEventLoopGroup-1-5] DEBUG i.m.h.server.netty.NettyHttpServer - Server localhost:8082 Received Request: POST /api/user
14:21:10.881 [nioEventLoopGroup-1-5] DEBUG i.m.h.s.netty.RoutingInBoundHandler - Matching route POST - /api/user
14:21:10.889 [nioEventLoopGroup-1-5] DEBUG i.m.h.s.netty.RoutingInBoundHandler - Matched route POST - /api/user to controller class movie.list.service.ListController
14:21:10.929 [nioEventLoopGroup-1-5] TRACE i.m.h.server.netty.NettyHttpServer - Server received streaming message for argument [Publisher publisher]: {"userName":"tim","movies":[{"imdbId":"tt1285016","inCollection":"false"},{"imdbId":"tt0100502","inCollection":"true"}]}
14:21:10.950 [nioEventLoopGroup-1-5] TRACE i.m.h.server.netty.NettyHttpServer - Cannot convert message for argument [Publisher publisher] and value: {"userName":"tim","movies":[{"imdbId":"tt1285016","inCollection":"false"},{"imdbId":"tt0100502","inCollection":"true"}]}
14:21:10.955 [nioEventLoopGroup-1-5] TRACE i.m.h.server.netty.NettyHttpServer - Done receiving messages for argument: Publisher publisher
14:21:10.955 [nioEventLoopGroup-1-5] ERROR i.m.h.s.n.c.UnsatisfiedRouteHandler - POST /api/user (Bad Request): Required argument [Publisher publisher] not specified
14:21:10.968 [nioEventLoopGroup-1-5] DEBUG i.m.h.s.netty.RoutingInBoundHandler - Encoding emitted response object [ /publisher - Required argument [Publisher publisher] not specified] using codec: io.micronaut.jackson.codec.JsonMediaTypeCodec@5f78de22
在TRACE级别记录io.micronaut.http.server。如果需要其他级别的日志,请告诉我。