0

I have a REST service, which creates some elements in my database. These elements are created "statically" in the server method and are not given by the client, but will be then returned to the client after the operation is complete.

If I declare the service as PUT/POST (because I want to create new things) the client is required to send an object, which has to be null because the client has no object to send (everything is done in the server).

If I declare it as GET I won't need to send a null-object, but my GET-method will be creating things, which I don't expect from a GET-method

So, what will be the best approach to follow in such a case: PUT, POST or GET?

iberbeu
  • 15,295
  • 5
  • 27
  • 48
  • you just want to trigger those elements creation only on the client call ? How about creating elements initially before any request is made to you and expose a GET method to client by which you can return those ? – Naveen Aug 23 '16 at 06:50
  • What happens if you invoke the resource multiple times? Should the objects get created on every call (side-effect) or should the objects only get created once? In the primer case you can't use `PUT` or `GET` due to their idempotent semantics. In the latter case I'd prefer @NaveenAechan proposal – Roman Vottner Aug 23 '16 at 06:59
  • @NaveenAechan your approach could work in my case and it could avoid some problems like multiple creation, as Roman Vottner pointed out. I'll give it a try. Anyway I would like to know if I should use GET, POST or PUT for the approach I explained in the question – iberbeu Aug 23 '16 at 07:03
  • As you just return the data to user, I propose GET for you. – Naveen Aug 23 '16 at 07:05
  • @NaveenAechan I guess you are right. If you elaborate it in an answer I could accept it – iberbeu Aug 23 '16 at 07:43
  • Checkout my answer below. If that helps you, let me know. – Naveen Aug 23 '16 at 07:52

2 Answers2

2

Here is the way I was expecting this to be done.

  1. Use ServletContextListener and on context initialized you invoke your logic. Refer this link
  2. And for the user expose a url http://yoururl/get/created/stuff and return the elements which you have created earlier. May be pull from db and convert into json/xml/plain-text etc.

Some more references

Community
  • 1
  • 1
Naveen
  • 830
  • 9
  • 19
1

POST without payload is possible, an maybe responds to your approach.

Azimuts
  • 1,212
  • 4
  • 16
  • 35
  • 1
    Although POST without payload is possible according to the spec (just as GET with a payload), many proxies and webservers have trouble with it. If you aim for compatibility, it is advisable to at least allow for some content that will be ignored. – Tomas Aug 23 '16 at 08:00