Search code examples
javarestvert.xvertx3

Vertx model binding for my Rest API layer


I'm using Vertx 3, and I'm trying to find a good decoupled module that knows to turn query-string, headers and both content-type and body into a bean?

I know spring does that and various other frameworks as well, but I don't want to introduce a new framework i just want a super fast model binder that will either know to auto bind to a certain method or at least auto bind a certain class so i can invoke my rest method that currently accept one parameter, which is the model.

public ResponseBase query(QueryRequest model){ ... }

I don't mind adding annotations to the parameters etc.

Thanks!


Solution

  • Current my team use vertx Json.decodeValue to turn body (json string) to java class.

    MyClass body = Json.decodeValue(rc.getBodyAsString(), MyClass.class);
    

    to config Json to handle unknown properties, I setting

    Json.mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    

    for your query string, I think it is easy to write a class to convert it to a json string :)

    I also catch DecodeException on Json.decodeValue to re throw a 400 Bad Request error.