@POST in RESTful web service

前端 未结 2 2074
情深已故
情深已故 2020-12-23 14:11

I have been trying to understand @POST in RESTful web service using Jersey. I have gone through http://www.vogella.com/articles/REST/article.html for the same and was able t

2条回答
  •  独厮守ぢ
    2020-12-23 15:03

    Please find example below, it might help you

    package jersey.rest.test;
    
    import javax.ws.rs.DELETE;
    import javax.ws.rs.GET;
    import javax.ws.rs.HEAD;
    import javax.ws.rs.POST;
    import javax.ws.rs.PUT;
    import javax.ws.rs.Path;
    import javax.ws.rs.PathParam;
    import javax.ws.rs.core.Response;
    
    @Path("/hello")
    public class SimpleService {
        @GET
        @Path("/{param}")
        public Response getMsg(@PathParam("param") String msg) {
            String output = "Get:Jersey say : " + msg;
            return Response.status(200).entity(output).build();
        }
    
        @POST
        @Path("/{param}")
        public Response postMsg(@PathParam("param") String msg) {
            String output = "POST:Jersey say : " + msg;
            return Response.status(200).entity(output).build();
        }
    
        @POST
        @Path("/post")
        //@Consumes(MediaType.TEXT_XML)
        public Response postStrMsg( String msg) {
            String output = "POST:Jersey say : " + msg;
            return Response.status(200).entity(output).build();
        }
    
        @PUT
        @Path("/{param}")
        public Response putMsg(@PathParam("param") String msg) {
            String output = "PUT: Jersey say : " + msg;
            return Response.status(200).entity(output).build();
        }
    
        @DELETE
        @Path("/{param}")
        public Response deleteMsg(@PathParam("param") String msg) {
            String output = "DELETE:Jersey say : " + msg;
            return Response.status(200).entity(output).build();
        }
    
        @HEAD
        @Path("/{param}")
        public Response headMsg(@PathParam("param") String msg) {
            String output = "HEAD:Jersey say : " + msg;
            return Response.status(200).entity(output).build();
        }
    }
    

    for testing you can use any tool like RestClient (http://code.google.com/p/rest-client/)

提交回复
热议问题