23

I have RESTful API built on top of a MongoDB store, so it's nice that you can store arrays. It's straightforward to create a new resource like this:

POST /users { items: [ 1001, 1002, 1003 ] }

But how would the HTTP endpoint for adding a new item or removing an item would look like?

Right now, I have to specify the entire array, including elements that I don't want to touch:

PATCH /users/{id} { name: 'Bruce Wayne', items: [ 1001, 1002 ] }

Or pass in a mongodb query directly:

PATCH /users/{id}?query[$push][items]=1003

Is there a better way to do this?

Edit:

I like how StackMob's API does it. How do I update the name and remove an element from items at the same time though? For example, when I'm updating a bunch of the user's details on an admin dashboard? I don't think replacing the entire array is a good idea in mongodb?

4

2 に答える 2

-1

新しいリクエストを作成および削除するための REST 標準に従って --> POST -コレクション内の新しいリソースを作成し、DELETE -リソースを削除します。

Java の高レベルの HTTP エンドポイントが Jersey を使用してどのように見えるかの例を挙げることができます。HTTP パスが指定された Resource クラスと、さまざまな操作を行うメソッドの特定のパスを持つことができます。したがって、URL は次のようになります -- /rest/MyResource/Resource にリクエスト JSON または XML (入力データを含む) を伴う

これは、エントリ ポイントとなる Resource クラスのサンプルです (もちろん、このクラスの URL マッピングを行うには、web.xml で構成を行う必要があります) -->

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.DELETE;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.json.JSONObject;

public class SampleRESTServiceResource {

    /**
     * @param incomingJsonString
     * @return Response 
     */
    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public Response createNewResource(JSONObject myJson) {
        // Do a call to a DAO Implementation that does a JDBC call to insert into Mongo based on JSON
        return null;

    }

    /**
     * @param incomingJsonString
     * @return Return response 
     */
    @DELETE
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public Response deleteResource(JSONObject myJson) {
        // Do a call to a DAO Implementation that does a JDBC call to delete resource from  Mongo based on JSON
        return null;
    }
}

例を試してみたい場合は、このページを参照できます --> https://www.ibm.com/developerworks/library/wa-aj-tomcat/

于 2014-02-19T10:27:08.660 に答える