Suggested Pages

Tuesday, May 26, 2015

JAX-RS POST Example

Example of Rest Service with POST method


@EJB
private DeviceRepository deviceRepository;

@EJB
private ContactRepository contactRepository;


@Path(value = "/devices/{deviceID}/contacts")
@POST
@Consumes({ MediaType.APPLICATION_JSON })
public ContactResponse saveContact(@PathParam(value = "deviceID") String deviceID, ContactRequest contactRequest) {
  try {
    Device device= deviceRepository.findById(deviceID);
    Contact contact=createContact(contactRequest,device);
    contact=contactRepository.save(contact)
    return createResponse(contact);
  } catch (Exception e) {
    throw new WebApplicationException(Status.INTERNAL_SERVER_ERROR);
  } 
 }

No comments :

Post a Comment

Suggested Pages