在泽西岛查询参数

Mak*_*kov 3 java jersey

我有一个关于查询参数的问题..该参数的想法是什么..在下面的情况下,我需要查询参数?

@GET
@Produces("text/plain")
public String sayHello(@QueryParam("name") String name) {
    if (name != null) {
        // if the query parameter "name" is there
        return "Hello " + name + "!";
    }
    return "Hello World!";
}     
Run Code Online (Sandbox Code Playgroud)

std*_*bar 7

当您拥有定义如下的服务时,将使用@PathParam:

@POST
@Path("/update/{userCode}")
public Response update(@PathParam( "userCode" ) String userCode)
Run Code Online (Sandbox Code Playgroud)

在此示例中,URL将类似于http://hostname.tld/update/1234,其中"1234"将从URL的路径部分解析出来.

@QueryParam是指您的网址包含普通网址参数,例如@Partha建议:

@POST
@Path("/update")
public Response update(@QueryParam( "userCode" ) String userCode)
Run Code Online (Sandbox Code Playgroud)

这里的URL看起来像http://hostname.tld/update?userCode = 1234

你使用哪一个取决于你喜欢的风格.REST爱好者会告诉你,你永远不应该使用QueryParam版本.我更灵活一点 - QueryParam版本的优点是你不会被锁定为一个订单,只是名字.

但最终由您决定对您的应用程序更有意义.