如何创建带有输入参数的 Restful Web 服务?

发布于 2024-12-06 02:55:58 字数 1218 浏览 1 评论 0原文

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

基于肥皂的 方法Web 服务我会像这样调用它

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

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

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

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

and i can invoke it using
http://localhost:8088/JerseyJAXB/rest/todo

and I want to create a method like

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

In case of soap based web services i would invoke it like this

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

but I want to know how to invoke it using rest and also can I pass the parameters as I am doing in the above example when I use rest and jersey.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

生生漫 2024-12-13 02:55:58

你可以。
尝试这样的操作:

@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

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

定格我的天空 2024-12-13 02:55:58

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

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

但您将无法从普通 Web 浏览器发送 PUT(目前)。如果直接输入 URL,它将是 GET。

不过,从哲学​​上来说,这看起来应该是一个 POST。在 REST 中,您通常可以 POST 到公共资源 /todo(该资源在其中创建并返回新资源),或者 PUT 到专门标识的资源,例如 /todo/< ;id>,用于创建和/或更新。

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.

半窗疏影 2024-12-13 02:55:58

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

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

贪恋 2024-12-13 02:55:58

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

}

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 ;

}
自演自醉 2024-12-13 02:55:58

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

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





    }
}

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





    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文