Writing data to a database using a fully REST web service

How to create a REST web service to write a row to a data table. Use the following script:

The table is called Client - the data that will be inserted into the line will be the name, address, phone number, email address.

I think it is impossible to fully describe all this in Java or C #, and I would never have expected it, but here are the questions that pop up to my head when I prepare the coding:

  • What will the URI look like (for example, if you use this URL - http://www.example.com/ )?
  • What information will get into the HTTP shell?
  • Can I use POST when writing to the database this way?
  • Am I using a resource to store published data from a client? Is it even necessary if the data is still written to the database?
  • When the data that needs to be written to db is received by the server - how can I physically insert it into the database - do I call any method on the server to actually write the data (in Java)? - this does not seem to match the true REST architecture - avoids RPC calls.
  • Do I even have to worry about writing to the database - should I store my data as a resource?

As you can see, I need to clear a few questions in my head. Any help is greatly appreciated.

+3
source share
2 answers

First of all, I am not an expert in java and C #, and I don’t know exactly what it means that these languages ​​must support the REST design, but in general:

  • http://www.example.com/customers - customers are a collection of resources and you want to add a new resource to this collection

  • It depends on different things - you should probably set the header for the content type (according to the data format in which you submit the view), and set some authentication headers if you need it.

  • Yes, you always use POST to create a new entry in the resource collection.

  • I don’t quite understand this question, to be honest. What do you mean by "direct data entry into the database"?

  • REST is primarily a communication style between the server and the client. It does not say anything about how you should process the received data using it. The usual way modern web approaches (MVC style frameworks) are suitable is to route each REST action to a method of some class (usually an instance of the controller), where you process the received parameters (for example, save them in the database) and generate a response that will sent back.

For a brief and very clear introduction to REST, take a look at this short video .

+5
source

RESTful Web Services published by O'Reilly and Associates seems to match the one you are looking for.

As much as possible in Java, Sun has a page on it .

+3
source

All Articles