Java 使用Helidon 2.0.0-M2在POST方法上获取原始JSON
我在下面有一个POST端点。 我想访问处理程序方法中发送的原始JSON。 理想情况下,这可以作为字符串或转换为映射。 JSON中的数据可能会有所不同,我不想像口袋妖怪示例中那样将其强制转换为特定的类 我在下面尝试了两种方法,一种尝试从请求对象访问数据,另一种使用String.class处理程序。第一个日志记录以下错误“SEVERE org.eclipse.yasson.internal.Unmarshaller Thread[nioEventLoopGroup-3-2,10,main]:意外字符39位于(行号=1,列号=1,偏移量=0)” 第二个打印JSON的第一部分“{key1:value1,” 指挥所Java 使用Helidon 2.0.0-M2在POST方法上获取原始JSON,java,helidon,Java,Helidon,我在下面有一个POST端点。 我想访问处理程序方法中发送的原始JSON。 理想情况下,这可以作为字符串或转换为映射。 JSON中的数据可能会有所不同,我不想像口袋妖怪示例中那样将其强制转换为特定的类 我在下面尝试了两种方法,一种尝试从请求对象访问数据,另一种使用String.class处理程序。第一个日志记录以下错误“SEVERE org.eclipse.yasson.internal.Unmarshaller Thread[nioEventLoopGroup-3-2,10,main]:意外字符
curl -d '{"key1":"value1", "key2":"value2"}' -H "Content-Type: application/json" -X POST http://localhost:8090/datastore/type
方法1
.post("/type", this::addDataTypeItem)
private void addDataTypeItem(ServerRequest request, ServerResponse response) {
// get RAW JSON as String or Map of JSON contents
// this code does not work.
// SEVERE org.eclipse.yasson.internal.Unmarshaller Thread[nioEventLoopGroup-3-2,10,main]: Unexpected char 39 at (line no=1, column no=1, offset=0)
request.content().as(JsonObject.class)
.thenAccept(jo -> printValue(jo));
}
方法2
.post("/type", Handler.create(String.class, this::addDataTypeItem))
private void addDataTypeItem(ServerRequest request, ServerResponse response, String value) {
// get RAW JSON as String or Map of JSON contents
// below prints "value: '{key1:value1,"
System.out.println("value: "+value);
}
口袋妖怪示例
.post("/pokemon", Handler.create(Pokemon.class, this::insertPokemon))
private void insertPokemon(ServerRequest request, ServerResponse response, Pokemon pokemon) {
dbClient.execute(exec -> exec
.createNamedInsert("insert-pokemon")
.indexedParam(pokemon)
.execute())
.thenAccept(count -> response.send("Inserted: " + count + " values\n"))
.exceptionally(throwable -> sendError(throwable, response));
}
在POST处理程序方法中将JSON作为字符串或映射获取的最佳方法是什么
谢谢,方法2似乎很好:
public static void main(final String[] args) throws IOException {
WebServer.builder(Routing.builder()
.register("/datastore", rules ->
rules.post("/type",
Handler.create(String.class, (req, res, value) -> System.out.println("String json: " + value))
)
).build()
).build()
.start()
.thenAccept(ws -> System.out.println("curl -d '{\"key1\":\"value1\", \"key2\":\"value2\"}' " +
"-H \"Content-Type: application/json\" " +
"-X POST http://localhost:" + ws.port() + "/datastore/type")
);
}
调用curl后:
curl -d '{"key1":"value1", "key2":"value2"}' -H "Content-Type: application/json" -X POST http://localhost:50569/datastore/type
输出:
String json: {"key1":"value1", "key2":"value2"}
Hi方法2似乎工作得很好:
public static void main(final String[] args) throws IOException {
WebServer.builder(Routing.builder()
.register("/datastore", rules ->
rules.post("/type",
Handler.create(String.class, (req, res, value) -> System.out.println("String json: " + value))
)
).build()
).build()
.start()
.thenAccept(ws -> System.out.println("curl -d '{\"key1\":\"value1\", \"key2\":\"value2\"}' " +
"-H \"Content-Type: application/json\" " +
"-X POST http://localhost:" + ws.port() + "/datastore/type")
);
}
调用curl后:
curl -d '{"key1":"value1", "key2":"value2"}' -H "Content-Type: application/json" -X POST http://localhost:50569/datastore/type
输出:
String json: {"key1":"value1", "key2":"value2"}
谢谢。您的代码帮助指出了这个问题。Windows curl似乎有单引号的问题。一旦我将curl语句更改为
curl-d“{\'key1\”:\'value1\”,\'key2\:\'value2\“}”-H“内容类型:application/json”-X柱http://localhost:8090/datastore/type
它可以工作。谢谢。您的代码帮助指出了这个问题。Windows curl似乎有单引号的问题。一旦我将curl语句更改为curl-d“{\'key1\”:\'value1\”,\'key2\:\'value2\”}“-H”内容类型:application/json”-X柱http://localhost:8090/datastore/type
它可以工作。