Java 使用参数调用 RESTful Web 服务

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6561436/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 07:45:28  来源:igfitidea点击:

Invoke RESTful webservice with parameter

javaweb-servicesrestparameter-passing

提问by M.M

I have a simple RESTful web service that print "Hello World !" I'm using NetBeans and the code looks like:

我有一个简单的 RESTful Web 服务,可以打印“Hello World!” 我正在使用 NetBeans,代码如下所示:

package resource;

import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.Consumes;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.GET;
import javax.ws.rs.Produces;


@Path("simple")
public class SimpleResource {

    @Context
    private UriInfo context;

    /** Creates a new instance of SimpleResource */
    public SimpleResource() {
    }

    @GET
    @Produces("application/xml")
    public String getXml() {
        //TODO return proper representation object
        return "<greeting>Hello World !</greeting>";
    }

    @PUT
    @Consumes("application/xml")
    public void putXml(String content) {
    }
}

I call this web service from this URL : http://localhost:8080/WebService/resources/simple. Now, I want to send a parameter to this web service, then print this parameter after the "Hello world" message.

我从这个 URL 调用这个网络服务:http://localhost:8080/WebService/resources/simple。现在,我想向此 Web 服务发送一个参数,然后在“Hello world”消息之后打印此参数。

How can we do that?

我们怎么做?

Thanks!

谢谢!

采纳答案by Donal Fellows

The two main ways of handling a parameter in REST are via parsing the path and via extracting the query part.

在 REST 中处理参数的两种主要方式是解析路径和提取查询部分。

Path parameters

路径参数

These handle this case — /foo/{fooID}— where {fooID}is a template that will be replaced by the parameter you want:

这些处理这种情况——/foo/{fooID}其中{fooID}是一个模板,它将被您想要的参数替换:

@GET
@Produces("text/plain")
@Path("/foo/{fooID}")
public String getFoo(@PathParam("fooID") String id) {
    // ...
}

These are great for the case where you can consider the parameter to be describing a resource.

这些非常适合您可以将参数视为描述资源的情况。

Query parameters

查询参数

These handle this case — /?foo=ID— just like you'd get from doing traditional form processing:

这些处理这种情况——/?foo=ID就像你从传统的表单处理中得到的一样:

@GET
@Produces("text/plain")
@Path("/")
public String getFoo(@QueryParam("foo") String id) {
    // ...
}

These are great for the case where you consider the parameter to be describing an adjunct to the resource, and not the resource itself. The @FormParamannotation is extremely similar, except it is for handling a POSTed form instead of GET-style parameters

这些非常适合您认为参数描述资源的附属物而不是资源本身的情况。该@FormParam注释是非常相似的,除了它是用于处理发布形式,而不是GET风格参数

Other types of parameters

其他类型的参数

There are other types of parameter handling supported by the JAX-RS spec (matrix parameters, header parameters, cookie parameters) which all work in about the same way to the programmer, but are rarer or more specialized in use. A reasonable place to start exploring the details is the JAX-RS javadocitself, as that has useful links.

JAX-RS 规范还支持其他类型的参数处理(矩阵参数、标头参数、cookie 参数),它们的工作方式与程序员大致相同,但在使用中较少见或更专门。开始探索细节的合理位置是JAX-RS javadoc本身,因为它有有用的链接。

回答by Maurice

Try adding a Path annotation like this:

尝试添加这样的路径注释:

@javax.ws.rs.Path(“/bookstore/books/{bookId}”)

回答by Srijani Ghosh

The sample code for a web service which accepts parameters in URl will look like this:

接受 URl 中的参数的 Web 服务的示例代码如下所示:

@GET
@Path("/search")
public String getUserDetailsFromAddress(
              @QueryParam("name") String name) {
  return "Hello"+name;
}

and the URL will be like this:

网址将是这样的:

http://localhost:8080/searchapp/mysearch/search?name=Tom