Hi,
it looks like you are creating a array of value for “values” and it is expecting an array of array of value:
values.add(body.getValue(key))
versus
putArray("values", new JsonArray().addArray(new JsonArray()
.addString(message.body().getObject("body").getString("name"))
.addNumber(message.body().getObject("body").getNumber("age"))
hope this helps
--
Julien Viet
www.julienviet.com
On 19 Jan 2015 at 18:11:16,
rowa...@gmail.com (
rowa...@gmail.com) wrote:
> I'm trying to parse a JSON request body in Vert.x, then send it to the
> mod-mysql-postgresql mod. I'm wondering if my code is okay or that I'm
> missing some important piece of code to make it work.
>
> somewhere in my routematching code:
>
> JsonObject eventBusData = new JsonObject();
> request.bodyHandler((Buffer buffer) -> {
> JsonObject body = new JsonObject(buffer.toString());
> eventBusData.putObject("body", body);
> sendToController(controller, eventBusData, request);
> }
>
> public void sendToController(String controller, JsonObject eventBusData, HttpServerRequest
> request) {
> EventBus eb = vertx.eventBus();
> eb.send(controller, eventBusData, (Message message) -> {
> request.response().end(message.body().toString());
> });
> }
>
> then, in the controller:
>
> eb.registerHandler("articles.insert", (Message message) -> {
> JsonObject body = message.body().getValue("body");
> JsonArray fields = new JsonArray();
> JsonArray values = new JsonArray();
> for (String key : body.getFieldNames()) {
> fields.addString(key);
> values.add(body.getValue(key));
> }
> eb.send("mysql",
> new JsonObject()
> .putString("action", "insert")
> .putString("table", "articles")
> .putArray("fields", fields)
> .putArray("values", values)
> , (Message data) ->
> message.reply(data.body())
> );
>
> and my HTTP request body:
>
> {
> "name": "pannekoek",
> "age": 150,
> "archived": true
> }
>
> gives me the following response:
>
> Uncaught Exception for request {"action":"insert","table":"articles","fields":["name","age","archived"],"values":["pannekoek","150","true"]}
> java.lang.ClassCastException: java.lang.String cannot be cast to org.vertx.java.core.json.JsonArray
>
>
> the following piece of code does work, but it's rather static. I want the
> parsing to be dynamic, as in it shouldn't matter what's in the request
> body. The validation will be done later.
>
> eb.send("mysql",
> new JsonObject()
> .putString("action", "insert")
> .putString("table", "articles")
> .putArray("fields", new JsonArray().addString("name").addString("age"))
> .putArray("values", new JsonArray().addArray(new JsonArray()
> .addString(message.body().getObject("body").getString("name"))
> .addNumber(message.body().getObject("body").getNumber("age")))), (Message
> data) ->
> message.reply(data.body())
> );
>
> In the third code block from the beginning of the post, omitting .putArray("fields",
> fields) doesn't solve the problem.
> Omitting .putArray("values", values) gives a nullpointerexception as the
> mysql mod expects it.
>
> Could anyone point me to either an error in the Vert.x JSON API or more
> likely a parsing error in my code?
>
> --
> You received this message because you are subscribed to the Google Groups "vert.x" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to
vertx+un...@googlegroups.com.
> For more options, visit
https://groups.google.com/d/optout.
>