【问题标题】:How to create a Restful web service with input parameters?如何使用输入参数创建 Restful Web 服务?
【发布时间】:2011-09-22 12:05:59
【问题描述】:

我正在创建 RESTful Web 服务,我想知道如何使用输入参数创建服务以及如何从 Web 浏览器调用它。

例如

@Path("/todo")
public class TodoResource {
    // This method is called if XMLis request
    @PUT
    @Produces( {MediaType.APPLICATION_XML,MediaType.APPLICATION_JSON})
    public Todo getXML() {
        Todo todo = new Todo();
        todo.setSummary("This is my first todo");
        todo.setDescription("This is my first todo");
        return todo;
    }

我可以使用它来调用它 http://localhost:8088/JerseyJAXB/rest/todo

我想创建一个类似

的方法
@Path("/todo")
    public class TodoResource {
        // This method is called if XMLis request
        @PUT
        @Produces( {MediaType.APPLICATION_XML,MediaType.APPLICATION_JSON})
        public Todo getXML(String x, String y) {
            Todo todo = new Todo();
            todo.setSummary(x);
            todo.setDescription(y);
            return todo;
        }

如果是基于肥皂的网络服务,我会像这样调用它

http://localhost:8088/JerseyJAXB/rest/todo?x=abc&y=pqr

但我想知道如何使用 rest 调用它,并且当我使用 rest 和 jersey 时,我是否可以像上面示例中所做的那样传递参数。

【问题讨论】:

    标签: java rest web-services jax-rs


    【解决方案1】:

    你可以。 试试这样的:

    @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;
    }
    

    然后使用此 URL 调用您的服务;
    http://localhost:8088/JerseyJAXB/rest/todo/summary/description

    【讨论】:

      【解决方案2】:

      如果需要查询参数,请使用@QueryParam

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

      但您将无法从普通网络浏览器发送 PUT(今天)。如果您直接输入 URL,它将是一个 GET。

      不过,从哲学​​上讲,这看起来应该是一个 POST。在 REST 中,您通常要么 POST 到公共资源 /todo,该资源在其中创建并返回新资源,要么您 PUT 到特定标识的资源,例如 /todo/<id>,以进行创建和/或更新。

      【讨论】:

      • 这个答案最好地解决了原始问题中提到的 URL 参数。参数或路径元素是否更适合此目的是另一个问题。
      【解决方案3】:

      小心。为此,您需要@GET(而不是@PUT)。

      【讨论】:

        【解决方案4】:

        另一种方法是获取 UriInfo 而不是所有 QueryParam

        然后您将能够根据代码中的需要获取 queryParam

        @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 ;
        
        }
        

        【讨论】:

          【解决方案5】:

          你可以试试这个...把参数设置为:
          http://localhost:8080/WebApplication11/webresources/generic/getText?arg1=hello 在您的浏览器中...

          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) {
          
          
          
          
          
              }
          }
          

          【讨论】:

            【解决方案6】:

            我们可以使用 Pathparam、QueryParam 和 HeaderParam 在 restful webservice 中发送输入参数

            1. PathParam 方法:在这个方法中,我们在 url 中发送输入为 localhost:8080/JerseyDemo/rest/user/{id} 并使用@PathParam 注释获取控制器中路径变量的值,如下所示

              @GET
              @Path("/{id}")
              public Response addUser(@PathParam("id") int id) {
              
              }
              
            2. QueryParam 方法:在此方法中,我们在 url 中的查询参数中发送输入参数为 localhost:8080/JerseyDemo/rest/user?p=ID,我们将使用 @QueryParam 注释在控制器中获取此查询变量值。

              @GET
              @Path("/user")
              public Response addUser(@QueryParam("p") int page) {
              
               }
              
            3. HeaderParam 方法:在这个方法中,我们在 header 中发送输入,并在 @HeaderParam 注释的帮助下读入控制器

               @POST
               public Response addUser(@HeaderParam("token") String token) {
              
               }
              

            参考文献Java Jersey Tutorials

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2018-04-10
              • 2011-02-12
              • 1970-01-01
              • 1970-01-01
              • 2017-03-26
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多