Web Services 如何使用输入参数创建REST风格的Web服务?

hjzp0vay  于 2022-11-15  发布在  其他
关注(0)|答案(5)|浏览(206)

我正在创建REST 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那样传递参数。

lvmkulzt

lvmkulzt1#

你可以试试这样的方法:

@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

kxe2p93d

kxe2p93d2#

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

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

但是你不能从普通的网页浏览器发送PUT(现在),如果你直接输入URL,它将是一个GET。
在REST中,通常要么POST到一个公共资源/todo,在那里该资源创建并返回一个新资源,要么PUT到一个特定标识的资源,比如/todo/<id>,用于创建和/或更新。

hi3rlvi2

hi3rlvi23#

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

b5buobof

b5buobof4#

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

}
8mmmxcuj

8mmmxcuj5#

您可以尝试此操作...将参数设置为:
浏览器中的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) {



    }
}

相关问题