How to create a Restful web service with input parameters?

前端 未结 5 705
花落未央
花落未央 2020-12-24 02:47

I am creating restful web service and i wanted to know how do we create a service with input parameters and also how to invoke it from a web browser.

For example

相关标签:
5条回答
  • 2020-12-24 03:24

    If you want query parameters, you use @QueryParam.

    public Todo getXML(@QueryParam("summary") String x, 
                       @QueryParam("description") String y)
    

    But you won't be able to send a PUT from a plain web browser (today). If you type in the URL directly, it will be a GET.

    Philosophically, this looks like it should be a POST, though. In REST, you typically either POST to a common resource, /todo, where that resource creates and returns a new resource, or you PUT to a specifically-identified resource, like /todo/<id>, for creation and/or update.

    0 讨论(0)
  • 2020-12-24 03:24

    You can try this... put parameters as :
    http://localhost:8080/WebApplication11/webresources/generic/getText?arg1=hello in your browser...

    package newpackage;
    
    import javax.ws.rs.core.Context;
    import javax.ws.rs.core.UriInfo;
    import javax.ws.rs.PathParam;
    import javax.ws.rs.Produces;
    import javax.ws.rs.Consumes;
    import javax.ws.rs.DefaultValue;
    
    
    import javax.ws.rs.GET;
    import javax.ws.rs.Path;
    import javax.ws.rs.PUT;
    import javax.ws.rs.QueryParam;
    
    @Path("generic")
    public class GenericResource {
    
        @Context
        private UriInfo context;
    
        /**
         * Creates a new instance of GenericResource
         */
        public GenericResource() {
        }
    
        /**
         * Retrieves representation of an instance of newpackage.GenericResource
    
         * @return an instance of java.lang.String
         */
        @GET
        @Produces("text/plain")
        @Consumes("text/plain")
        @Path("getText/")
        public String getText(@QueryParam("arg1")
                @DefaultValue("") String arg1) {
    
           return  arg1 ;  }
    
        @PUT
        @Consumes("text/plain")
        public void putText(String content) {
    
    
    
    
    
        }
    }
    
    0 讨论(0)
  • 2020-12-24 03:28

    Be careful. For this you need @GET (not @PUT).

    0 讨论(0)
  • 2020-12-24 03:30

    You can. Try something like this:

    @Path("/todo/{varX}/{varY}")
    @Produces({"application/xml", "application/json"})
    public Todo whatEverNameYouLike(@PathParam("varX") String varX,
        @PathParam("varY") String varY) {
            Todo todo = new Todo();
            todo.setSummary(varX);
            todo.setDescription(varY);
            return todo;
    }
    

    Then call your service with this URL;
    http://localhost:8088/JerseyJAXB/rest/todo/summary/description

    0 讨论(0)
  • 2020-12-24 03:44

    another way to do is get the UriInfo instead of all the QueryParam

    Then you will be able to get the queryParam as per needed in your code

    @GET
    @Path("/query")
    public Response getUsers(@Context UriInfo info) {
    
        String param_1 = info.getQueryParameters().getFirst("param_1");
        String param_2 = info.getQueryParameters().getFirst("param_2");
    
    
        return Response ;
    
    }
    
    0 讨论(0)
提交回复
热议问题