简体   繁体   中英

How to get Restangular to PUT without ID?

I'm working on the front-end for a web application that has a somewhat unorthodox "RESTful" back-end. The CRUD actions look like this:

model
  GET    :id
  POST   {payload}
  PUT    {payload}
  DELETE :id

In other words, the PUT action does not have an ID. Instead, the back-end retrieves the ID from the payload object. This creates a problem when Restangular attempts to generate a URL for a single resource:

Restangular.one('model', 2).get().then ($model) ->
  $model.name = 'newName'
  $model.save()

The request then generated by Restangular is, of course:

PUT /model/2

When I actually just need it to be

PUT /model

Is there any programmatically reliable way to accomplish this? It only applies to updating records. Thanks!

If you pass the entire object in the body of a PUT, the same way you do with a POST, you shouldn't have to have an id value. What you would have to do is identify the correct record for updating form the body of the code. Something like this in the Service layer:

//Get Disposition Methods for test app
    //Method which should return a single Disposition object in JSON format
    @GET
    @Path("/disposition/{id}")
    @Produces(MediaType.APPLICATION_JSON)
    public Disposition getDispositionById(@PathParam("id") int id){
    . . .
    }


    //Method which should return a list of all Dispositions in JSON format
    @GET
    @Path("/disposition")
    @Produces(MediaType.APPLICATION_JSON)
    public List<Disposition> getAllDispositions(){
     . . .
    }

    //Method to delete a specific Disposition
    @DELETE
    @Path("/disposition/{id}")
    public void deleteDispositionById(@PathParam("id") int id){
     . . .
    }

    //Creation of a new Disposition
    @POST
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @Path("/disposition")
    public Response createNewDisposition(String body){
        Disposition disposition = new Disposition();
        log.info("---- POST"+body);
        body = "["+body+"]";
        try{
         . . .//create Java object from body of post

       }catch(JSONException e){
        . . .
       }
        try{
         . . .//save new object
        }
        catch(Exception e){
        . . .
        }

    }

    //update an existing Disposition
    @PUT
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @Path("/disposition")
    public Response updateNewDisposition(String body){
        Disposition disposition = null;
        log.info("---- Put "+body);
        body = "["+body+"]";
        try{
         . . . .//find and update record

       }catch(JSONException e){
          . . . .
       }

    }

As you can see, the PUT in this case doesn't include an id tag. Not sure how efficient it is, but it works.

Ted

You can do it like this:

Restangular.one('model').customPUT($model.plain());

With above code and Restangular.service and Restangular.extendModel you can define your own save() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM