繁体   English   中英

如何创建带入参的Restful web服务?

[英]How to create a Restful web service with input parameters?

我正在创建宁静的 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;
        }

如果是基于 soap 的 web 服务,我会这样调用它

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

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

您可以。 尝试这样的事情:

@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://本地主机:8088 / JerseyJAXB / REST / TODO /摘要/描述

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

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

但是你将无法从普通的网络浏览器(今天)发送PUT。 如果直接输入URL,则为GET。

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

另一种方法是获取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 ;

}

小心。 为此你需要@GET(不是@PUT)。

你可以尝试这个...把参数作为:
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) {





    }
}

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

  1. PathParam 方法:在此方法中,我们将输入作为 localhost:8080/JerseyDemo/rest/user/{id} 在 url 中发送,并使用 @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 方法:在此方法中,我们在标头中发送输入并借助 @HeaderParam 注释在控制器中读取

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

参考Java Jersey 教程

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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