如何在 Java Jersey 应用程序中使用查询参数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41455544/
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
How to use the query parameters in Java Jersey Application?
提问by Amresh Kadian
I am following a tutorial and also used the Stackoverflow question here. Here is my Java class:
我正在学习教程,并在此处使用了 Stackoverflow 问题。这是我的 Java 类:
package com.crunchify.tutorial;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Consumes;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.UriInfo;
import org.json.simple.JSONObject;
@Path("api")
public class CrunchifyAPI {
@SuppressWarnings("unchecked")
@GET
@Path("/get")
@Consumes(MediaType.TEXT_PLAIN)
public String get(
@DefaultValue("111") @QueryParam("user") int user,
@Context UriInfo uriInfo
) {
MultivaluedMap<String, String> queryParams = uriInfo.getQueryParameters();
String nameParam = queryParams.getFirst("user");
System.out.println("Data Received: " + uriInfo.getRequestUri().getQuery()
+ " | " + nameParam);
JSONObject obj = new JSONObject();
obj.put("auth", true);
String ret = JSONObject.toJSONString(obj);
return ret;
}
}
Following is what I am GET'ing from postman:
以下是我从邮递员那里得到的:
GET>> localhost/api/get?user=123
Responseis:
回应是:
{"auth":true}
Server console:
服务器控制台:
Starting Crunchify's Embedded Jersey HTTPServer...
Started Crunchify's Embedded Jersey HTTPServer Successfully !!!
Data Received: ?user=123 | null
User Authenticated: true
I have tried with passing String, Integer etc but nothing works. The uri Info is getting printed correctly and the response back is also fine. The issue is that I am not getting the parameter to be read in Java Code. I will need to pass many other parameters once I am able to get this going. Please suggest. Thanks!!
我尝试过传递字符串、整数等,但没有任何效果。uri 信息打印正确,响应也很好。问题是我没有在 Java 代码中读取要读取的参数。一旦我能够做到这一点,我将需要传递许多其他参数。请建议。谢谢!!
回答by Nielsvh
I think you're trying too hard. As far as I can tell, doing the following should get you what you want if you call localhost/api/get?user=123:
我觉得你太努力了。据我所知,如果您调用 localhost/api/get?user=123,执行以下操作应该可以得到您想要的结果:
package com.crunchify.tutorial;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Consumes;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.UriInfo;
import org.json.simple.JSONObject;
@Path("api")
public class CrunchifyAPI {
@SuppressWarnings("unchecked")
@GET
@Path("/get")
@Consumes(MediaType.TEXT_PLAIN)
public String get(
@DefaultValue("111") @QueryParam("user") Integer user,
@Context UriInfo uriInfo
) {
System.out.println("Data Received: " + uriInfo.getRequestUri().getQuery()
+ " | " + name);
JSONObject obj = new JSONObject();
obj.put("auth", true);
String ret = JSONObject.toJSONString(obj);
return ret;
}
}
All that extra stuff with the query string isn't needed if all you need is the information passed in the user parameter.
如果您只需要用户参数中传递的信息,则不需要查询字符串的所有额外内容。
回答by Kalpesh Soni
@QueryParam("user") int user
@QueryParam("user") int 用户
the value of that user int should be 123
该用户 int 的值应为 123
See https://www.mkyong.com/webservices/jax-rs/jax-rs-queryparam-example/
见https://www.mkyong.com/webservices/jax-rs/jax-rs-queryparam-example/
回答by Kalpesh Soni
Well, I think you're having a problem with Java Types.
好吧,我认为您在使用 Java 类型时遇到了问题。
If your user is an Integer
you should pass it to String
first if you want to work with a String
(Integer.toString()
or String.valueof()
).
如果您的用户是 a Integer
,String
如果您想使用String
(Integer.toString()
或String.valueof()
) ,您应该首先将其传递给。
But the way you're passing the parameter is bothering me, I'm not sure if you can pass integers by text plain medi types.
但是您传递参数的方式让我感到困扰,我不确定您是否可以通过文本纯 medi 类型传递整数。