使用完全REST Web服务将数据写入数据库 [英] Writing data into a database using a fully REST web service

查看:321
本文介绍了使用完全REST Web服务将数据写入数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何创建REST Web服务以将行写入数据库表。使用以下方案:


表称为 Customer - 要插入的
数据


我认为这是不可能的描述整个事情以Java或C#结束,我永远不会期望,但这里有我在我准备编码的问题:




  • URI的外观(例如,如果您使用此网址 - http:// www .example.com / )?

  • HTTP信封中会包含什么信息?

  • 数据库以这种方式?

  • 我使用资源来存储从客户端发布的数据?如果数据正在写入数据库,这是否还是必要的?

  • 当要写入数据库的数据被服务器接收时 - 如何物理地插入数据库 - 我在服务器上调用一些方法来实际写入数据(在Java中)? - 这似乎不符合真正的REST架构 - 避开RPC调用。

  • 我应该打算写一个DB - 数据作为资源?



正如你所看到的,我需要清除一些问题。任何帮助非常感激。

解决方案

首先,我不是java或c#专家,这些语言必须支持REST设计,但一般来说,这意味着什么:


  1. http://www.example.com/customers - customers是资源集合,您要向此集合添加新资源


  2. 这取决于各种事情 - 您应该设置content-type标头(根据您发送表示的数据格式),并设置一些认证头


  3. 是的,您总是使用POST在资源集合中创建新条目。

    b
  4. 我不完全明白这个问题,老实说。


  5. REST主要只是服务器和客户端之间的通信方式。它不说任何关于你应该如何处理使用它收到的数据。现代web方法(MVC样式框架)解决它的通常方式是通过将每个REST操作路由到某个类(通常是控制器实例)的方法,在该方法中您处理接收到的参数(例如,将它们保存到数据库)和生成


  6. $ b $ p

    有关REST的简短非常清晰的介绍, a href =http://www.youtube.com/watch?v=YCcAE2SCQ6k =nofollow noreferrer>短片。


    How would one create a REST web service to write a row into a databse table. Use the follwoing scenario:

    The table is called Customer - the data to be inserted into the row would be the name, addresss, telephone number, email.

    I think its impossible to describe the whole thing end to end in Java or C#, and I would never expect that, but here are the questions I have popping into my head as I prepare for coding:

    • How the URI would look (eg. if you use this URL - http://www.example.com/)?
    • What info would go into the HTTP envelope?
    • Would I use POST when writing to the database in this way?
    • Do I use a resource to store the posted data from the client? Is this even necessary if the data is being written to a database anyway?
    • When the data to be writeen into the db is recieved by the server - how do I physically insert it into the database - do I call some method on the server to actually write the data (in Java)? - this doesn't seem to fit with truely REST architecture - shunning RPC calls.
    • Should I even be bothering writing to a DB - should I be storing my data as a resource?

    As you can see I need a few issues clearing in my head. Any help much appreciated.

    解决方案

    First of all, I'm not either java nor c# expert and I don't exactly know what means do these languages have to support REST design, but in general:

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

    2. It depends on various things - you should probably set the content-type header (according to the data format in which you are sending the representation) and set some authentication headers if you need it.

    3. Yes, you always use POST to create a new entry in a collection of resources.

    4. I don't fully understand this question, to be honest. What do you mean by "inmediately writing data into the database"?

    5. REST is primarily just a style of communication between server and a client. It doesn't say anything about how you should handle the data received by using it. The usual way how modern web approaches (MVC style frameworks) solve it, is by routing every REST action to a method of some class (usually a controller instance) where you handle the received parameters (eg. save them to the database) and generate a response to be sent back.

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

    这篇关于使用完全REST Web服务将数据写入数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆