如何使用输入参数创建Restful Web服务? [英] How to create a Restful web service with input parameters?

查看:81
本文介绍了如何使用输入参数创建Restful Web服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建宁静的Web服务,我想知道我们如何创建带有输入参数的服务,以及如何从Web浏览器调用它.

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.

例如

@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服务的情况下,我会这样调用它

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

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

但是我想知道如何使用rest调用它,也可以像上例中那样使用rest和jersey时传递参数.

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.

推荐答案

可以. 尝试这样的事情:

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

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

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

这篇关于如何使用输入参数创建Restful Web服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆