Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
203 views
in Technique[技术] by (71.8m points)

sql - Need help about @Get @Post @Put @Delete in Restful webservice

I need your help and advice. This is my first project in jersey. I don't know much about this topic. I'm still learning. I created my school project. But I have some problems on the web service side. Firstly I should explain my project. I have got 3 tables in my database. Movie,User,Ratings
Here my Movie table columns. I will ask you some points about Description column of the Movie table. I will use these methods to these columns.
Movie= Description (get,put,post and delete) I have to use all methods in this page.
             movieTitle (get)
             pictureURL (get,put)
             generalRating (get,post)

I built my Description page. But I'm not sure if its working or not .(My database is not ready to check them). Here is my page. I wrote this page, looking at the example pages. Can u help me to find the problems and errors. I just want to do simple methods get(just reading data), post(update existing data), put(create new data), delete(delete specific data) these things.What should I do now, is my code okay or do you have alternative advice? :( I need your help guys ty

package com.vogella.jersey.first;

import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.util.List;
import javax.ejb.*;
import javax.persistence.*;
import javax.ws.rs.*;
import javax.ws.rs.DELETE;
import javax.ws.rs.FormParam;
import javax.ws.rs.OPTIONS;
import javax.ws.rs.PUT;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Context;




@Path("/Movie/Description")
public class Description {

    private Moviedao moviedao = new Moviedao();

@GET
@Path("/Description/")
@Produces(MediaType.APPLICATION_XML)
public Description getDescriptionID(@PathParam("sample6") string sample6){
        return moviedao.getDescriptionID(id);
}

@POST
@Path("/Description/")
@Produces(MediaType.APPLICATION_XML)
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public void updateDescription(@PathParam("sampleID")int sampleID,
        @PathParam("sample2Description")string sample2Description) 
        throws IOException  {
          Description description = new Description (sampleID, sample2Description);
          int result = moviedao.updateDescription(description);
          if(result == 1){
             return SUCCESS_RESULT;
          }
          return FAILURE_RESULT;
}

@PUT
@Path("/Description")
@Produces(MediaType.APPLICATION_XML)
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public String createUser(@FormParam("sample8ID") int sample8ID,
   @FormParam("sample8Description") String sample8Description,
   @Context HttpServletResponse servletResponse) throws IOException{
   Description description = new Description (sample8ID, sample8Description);
   int result = movidao.addDescription(description);
   if(result == 1){
      return SUCCESS_RESULT;
   }
   return FAILURE_RESULT;
}

@DELETE
@Path("/Description/{descriptionID}")
@Produces(MediaType.APPLICATION_XML)
public String deleteUser(@PathParam("descriptionID") int descriptionID){
   int result = moviedao.deleteDescription(descriptionID);
   if(result == 1){
      return SUCCESS_RESULT;
   }
   return FAILURE_RESULT;
}

@OPTIONS
@Path("/Description")
@Produces(MediaType.APPLICATION_XML)
public String getSupportedOperations(){
   return "<operations>GET, PUT, POST, DELETE</operations>";
}


}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I just want to do simple methods get(just reading data), post(update existing data), put(create new data), delete(delete specific data) these things

POST should be used to create resources and PUT should be used to update resources.

Your class already has webservice path /Movie/Description, so there is no need to repeat word Description in the methods.

Also, I would recommend keep path names in lower case e.g. /movie/description


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...