Mixing structured and un-structured data with JSON-B / JSON-P, JAX-RS and Java EE 8 📎
import javax.json.JsonObject;
public class Attendee {
public String firstName;
public String lastName;
public long age;
public JsonObject profile;
@Override
public String toString() {
return "Attendee{" + "firstName=" (...) profile=" + profile + '}';
}
}
Is marshalled by JAX-RS without any additional configuration:
import com.airhacks.attendees.entity.Attendee;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
@Path("attendees")
public class AttendeesResource {
@POST
public void registry(Attendee attendee) {
System.out.println("attendee = " + attendee);
}
}
The command: url -i -XPOST -H'Content-type: application/json' -d"@attendee.json" http://localhost:8080/jsonp-with-jsonb/resources/attendees
with the payload (file attendee.json):
{
"firstName": "duke",
"lastName": "java",
"age": 21,
"profile": {
"languages": ["java", "javascript"],
"size": 10,
"kind": "alien"
}
}
yields:
attendee = Attendee{firstName=duke, lastName=java, age=21, profile={"languages":["java","javascript"],"size":10,"kind":"alien"}}]]
in the server log.
See you at Web, MicroProfile and Java EE Workshops at Munich Airport, Terminal 2 or Virtual Dedicated Workshops / consulting. Is Munich's airport too far? Learn from home: airhacks.io.