如何创建带有输入参数的 Restful Web 服务?
我正在创建宁静的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
你可以。
尝试这样的操作:
然后使用此 URL 调用您的服务;
http://localhost:8088/JerseyJAXB/rest/todo/summary/description
You can.
Try something like this:
Then call your service with this URL;
http://localhost:8088/JerseyJAXB/rest/todo/summary/description
如果您需要查询参数,请使用
@QueryParam
。但您将无法从普通 Web 浏览器发送 PUT(目前)。如果直接输入 URL,它将是 GET。
不过,从哲学上来说,这看起来应该是一个 POST。在 REST 中,您通常可以 POST 到公共资源
/todo
(该资源在其中创建并返回新资源),或者 PUT 到专门标识的资源,例如/todo/< ;id>
,用于创建和/或更新。If you want query parameters, you use
@QueryParam
.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.当心。为此,您需要@GET(而不是@PUT)。
Be careful. For this you need @GET (not @PUT).
另一种方法是获取 UriInfo 而不是所有 QueryParam
然后您将能够根据代码中的需要获取 queryParam
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
您可以尝试这个...将参数设置为:
http://localhost:8080/WebApplication11/webresources/generic/getText?arg1=你好
在你的浏览器中...
You can try this... put parameters as :
http://localhost:8080/WebApplication11/webresources/generic/getText?arg1=hello
in your browser...