I would like to have a service that responds to POST requests to / contact with the following payload:
{"records":[{"firstname":"John","lastname":"Doe"}]}
Ideally, records should be a wrapper for all types: Contact, Order, etc. So I would like to use a generic type, but Jersey doesn't seem to be able to untie it. Here is my code:
@Controller
@Path("/contact")
public class ContactResource {
@Autowired
private ContactService contactService;
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public List<Contact> saveContact(final Records<Contact> contact) {
return Arrays.asList(contactService.saveContact(contact.records.get(0)));
}
}
@XmlRootElement
public class Records<T> {
public List<T> records;
}
It seems that using custom javax.ws.rs.ext.MessageBodyReader might solve my problem. Correctly?
source
share