Write Scenario

How To Guide for building a Sample OData service with the OData Library (Java)

This How To Guide shows how to create, update and delete an entry via your Data Provider (ODataSingleProcessor).

Prerequisites

This tutorial is based on the Read Scenario - OData Library (Java) tutorial.

Implementing create, update and delete entry at the single processor

Create entry

Sample Code

@Override
public ODataResponse createEntity(PostUriInfo uriInfo, InputStream content, 
String requestContentType, String contentType) throws ODataException {
  //No support for creating and linking a new entry
  if (uriInfo.getNavigationSegments().size() > 0) {
  throw new ODataNotImplementedException();
  }

  //No support for media resources
  if (uriInfo.getStartEntitySet().getEntityType().hasStream()) {
  throw new ODataNotImplementedException();
  }

  EntityProviderReadProperties properties = EntityProviderReadProperties.init().mergeSemantic(false).build();

  ODataEntry entry = EntityProvider.readEntry(requestContentType, uriInfo.getStartEntitySet(), content, properties);
  //if something goes wrong in deserialization this is managed via the ExceptionMapper
  //no need for an application to do exception handling here an convert the exceptions in HTTP exceptions

  Map<String, Object> data = entry.getProperties();
  //now one can use the data to create the entry in the backend ...
  //retrieve the key value after creation, if the key is generated by the server

  //update the data accordingly
  data.put("Id", Integer.valueOf(887788675));

  //serialize the entry, Location header is set by OData Library
  return EntityProvider.writeEntry(contentType, uriInfo.getStartEntitySet(), entry.getProperties(), EntityProviderWriteProperties.serviceRoot(getContext().getPathInfo().getServiceRoot()).build());
}
Update entry

Sample Code

@Override
public ODataResponse updateEntity(PutMergePatchUriInfo uriInfo, InputStream content, String requestContentType, boolean merge, String contentType) throws ODataException {
EntityProviderReadProperties properties = EntityProviderReadProperties.init().mergeSemantic(false).build();

  ODataEntry entry = EntityProvider.readEntry(requestContentType, uriInfo.getTargetEntitySet(), content, properties);
  //if something goes wrong in deserialization this is managed via the ExceptionMapper,
  //no need for an application to do exception handling here an convert the exceptions in HTTP exceptions

  Map<String, Object> data = entry.getProperties();

  if ("Cars".equals(uriInfo.getTargetEntitySet().getName())) {
  int key = getKeyValue(uriInfo.getKeyPredicates().get(0));

    //if there is no entry with this key available, one should return "404 Not Found"
    //return ODataResponse.status(HttpStatusCodes.NOT_FOUND).build();

    //now one can use the data to create the entry in the backend ...
    String model = (String) data.get("Model");
    //...
  } else if ("Manufacturers".equals(uriInfo.getTargetEntitySet().getName())) {
  int key = getKeyValue(uriInfo.getKeyPredicates().get(0));
  //now one can use the data to create the entry in the backend ...
  }

  //we can return Status Code 204 No Content because the URI Parsing already guarantees that
  //a) only valid URIs are dispatched (also checked against the metadata)
  //b) 404 Not Found is already returned above, when the entry does not exist 
return ODataResponse.status(HttpStatusCodes.NO_CONTENT).build();
}
Delete entry

Sample Code

@Override
public ODataResponse deleteEntity(DeleteUriInfo uriInfo, String contentType) throws ODataException {
  if ("Cars".equals(uriInfo.getTargetEntitySet().getName())) {
  int key = getKeyValue(uriInfo.getKeyPredicates().get(0));

    //if there is no entry with this key available, one should return "404 Not Found"
    //return ODataResponse.status(HttpStatusCodes.NOT_FOUND).build();

    //now one can delete the entry with this particular key in the backend...

  } else if ("Manufacturers".equals(uriInfo.getTargetEntitySet().getName())) {
  int key = getKeyValue(uriInfo.getKeyPredicates().get(0));
  //now one can delete the entry with this particular key in the backend...
  }

  //we can return Status Code 204 No Content because the URI Parsing already guarantees that
  //a) only valid URIs are dispatched (also checked against the metadata)
  //b) 404 Not Found is already returned above, when the entry does not exist 
  return ODataResponse.status(HttpStatusCodes.NO_CONTENT).build();
}

Test your Service

After the implementation of the MyODataSingleProcessor the web application can be tested.

Copyright © 2013-2023, The Apache Software Foundation
Apache Olingo, Olingo, Apache, the Apache feather, and the Apache Olingo project logo are trademarks of the Apache Software Foundation.

Privacy