Java 如何在 URL 中传递两个查询参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30891057/
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-11 10:20:11  来源:igfitidea点击:

How to pass two query parameters in URL

javaweb-servicesrestjakarta-eejax-rs

提问by alle3x

In this example, the URL for a service has the form /projection/projectionId:

在此示例中,服务的 URL 具有以下形式/projection/projectionId

@Stateless
@Path("projection")
public class ProjectionManager {

@Inject
private ProjectionDAO projectionDAO;

@Inject
private UserContext userContext;

@GET
@Path("{projectionId}")
@Produces("application/json")
public String places(@PathParam("projectionId") String projectionId) {
    return projectionDAO.findById(Long.parseLong(projectionId)).getPlaces().toString();
}}

How can I pass two (or more) query parameters to access the service using this code:

如何使用以下代码传递两个(或更多)查询参数来访问服务:

@PUT
@Path("/buy")
public Response buyTicket(@QueryParam("projectionId") String projectionId, @QueryParam("place") String place) {
    Projection projection = projectionDAO.findById(Long.parseLong(projectionId));
    if(projection != null) {
        projectionDAO.buyTicket(projection, userContext.getCurrentUser(), Integer.parseInt(place));
    }

    return Response.noContent().build();
}

回答by Tomasz W

/buy?projectionId=value1&place=value2

Take a look at https://en.wikipedia.org/wiki/Query_stringfor further information. And since it is HTTP PUT you cannot simply open that URL in your browser, you can write some simple REST client or use browser extension like Postman in Chrome.

查看https://en.wikipedia.org/wiki/Query_string了解更多信息。由于它是 HTTP PUT,您不能简单地在浏览器中打开该 URL,您可以编写一些简单的 REST 客户端或使用 Chrome 中的 Postman 等浏览器扩展程序。

回答by Jiri Kremser

Query parameter is the thing after the ?in the URI, while path parameter is the parametrer before the ?in the URI.

查询参数是?在 URI之后的东西,而路径参数是?在 URI之前的参数。

If you need two inputs to your method, you can go with any combination of query param and path param => four combinations

如果您的方法需要两个输入,您可以使用查询参数和路径参数的任意组合 => 四种组合

It's a good convention that path params should denote some kind of identity of the resource, because it's part of it's address, while query params more some form/shape/filtering of the response.

路径参数应该表示资源的某种身份是一个很好的约定,因为它是它的地址的一部分,而查询参数更多的是响应的某种形式/形状/过滤。

In your case, I'd encode both params as path parameters, so the code would look like this:

在您的情况下,我会将两个参数都编码为路径参数,因此代码如下所示:

@PUT
@Path("/buy/{projectionId}/place/{place}")
public Response buyTicket(@PathParam("projectionId") String projectionId, @PathParam("place") String place){
    Projection projection = projectionDAO.findById(Long.parseLong(projectionId));
    if(projection != null){
        projectionDAO.buyTicket(projection, userContext.getCurrentUser(), Integer.parseInt(place));
    }

    return Response.noContent().build();
}

The URL would look like: ${host}/buy/1337/place/42

该 URL 将如下所示: ${host}/buy/1337/place/42

回答by Malusi Ninela

Thanks for your input guys, I have fixed it.

感谢您的输入,我已修复它。

It looks like I had to add the path parameter to the additional parameters, and pass additional parameters on the request, instead of the path parameter. Code looks as below,

看起来我必须将路径参数添加到附加参数中,并在请求中传递附加参数,而不是路径参数。代码如下所示,

it('should get a customer, searches with a customer name', (done) => {
var pathParams = {};
var body = {};
var additionalParams = {
queryParams: {
name: 'Ominathi'
}
};
//apigClient.invokeApi(pathParams, '/customer', 'GET', queryParams, body)
apigClient.invokeApi(pathParams, '/customer', 'GET', additionalParams, body)
.then(response => {
expect(response.status).toBe(200);
done();
})
.catch(err => {
fail(err);
done();
});
});

Thanks.

谢谢。

Ref: https://www.npmjs.com/package/aws-api-gateway-client

参考:https: //www.npmjs.com/package/aws-api-gateway-client