Restful way of sending Pre-Post or Pre-Put warning message

Tags: , , ,



I am trying to convert a legacy application to Restful web-services. One of our old form displayed a warning message immediately on form load. This warning message depends on a user property.

For example, if there is a property isInactiveByDefault which when set to “true” will set the status of the newly created employee via POST v1/employees to “Inactive”. User on “Employee” form load will see a warning message “Any new employee created will have inactive status”.

I originally thought of providing a resource to get the status of the property and let the client handle whether to display the warning message or not based on the value of the property. But my manager wants to avoid any business logic in client side. As per him, this is a business logic and should be handled from server.

I am wondering what’s the Restful way of sending this warning message. I could send the message with POST response but this would mean they will receive the message after the action.

Answer

I think that delegating the choice to display or not the message to the client is at the very frontier of what one may call “business logic”. I believe there is room for debate here..

But let aside this point, and if we consider the message(s) to display as data then as soon as your client can make REST calls (via JS, AJAX or whatever), you should be able to query the server before or while the form loads (and wait/sync on that).

So it is perfectly fine and “RESTful” to perform a GET request on the service to i.e. retrieve a list of warning messages (eventually internationalized) and display them. The server will prepare this list of warnings based on such or such property and return it (as JSON or whatever). Your client will only have to display the parsed result of the request (0-N messages to list) next to your form.

EDIT

Example REST service for your use case:

@POST
@Path("/v1/employees")
public Response someFormHandlingMethod() {
   // Your form processing
   ...
   if(EmployeeParameters.isInactiveByDefault()) {
      emp.setInactive(true);
   }
   ...
}

@GET
@Path("/v1/employees/formwarnings")
public Response getEmployeeFormWarnings() {
   // "Business logic" -> Determine warning messages to return to client
   ...
   if(EmployeeParameters.isInactiveByDefault()) {
      warnings.add("Any new employee created will have inactive status");
   }
   ...
   // and return them in Response (entity as JSON or whatever). Could also be List<String> for example.
}

It is just an example so that you get the idea but the actual implementation will depend on your Jersey configuration, mappers/converters, annotations (such as @Produces), etc.



Source: stackoverflow