Java JAX-RS @PathParam 如何传递带有斜杠、连字符和等号的字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2291428/
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
JAX-RS @PathParam How to pass a String with slashes, hyphens & equals too
提问by Sashi Kiran Challa
I am new to JAX-RS and I am trying to use Jersey to build a simple RESTful Webservice.
我是 JAX-RS 的新手,我正在尝试使用 Jersey 来构建一个简单的 RESTful Web 服务。
I have 2 questions. Please clarify these:
我有2个问题。请澄清这些:
I am trying to have my simple webservice like this URL
http://localhost:8080/SampleJersey/rest/inchi/InChIName
The InChIName is a string like this
InChI=1S/C9H8O4/c1-6(10)13-8-5-3-2-4-7(8)9(11)12/h2- 5H,1H3,(H,11,12)
. How do I pass this as a@PathParam
, I mean a normal String is working fine but here there are slashes,hyphens, and commas. How do I make it to ignore these. I tried putting it in quotes, but that didnt work. How should I do this?I need to pass that
InChI
to another webservice and that returns an XML as an output and I want to display that XML output as my Webservice's output. If I have@Produces("application/xml")
will it work?
我试图让我的简单网络服务像这个 URL
http://localhost:8080/SampleJersey/rest/inchi/InChIName
InChIName 是这样的字符串
InChI=1S/C9H8O4/c1-6(10)13-8-5-3-2-4-7(8)9(11)12/h2- 5H,1H3,(H,11,12)
。我如何将它作为 a 传递@PathParam
,我的意思是一个普通的 String 工作正常,但这里有斜杠、连字符和逗号。我如何让它忽略这些。我试着把它放在引号中,但这没有用。我该怎么做?我需要将它传递
InChI
给另一个 Web 服务并返回一个 XML 作为输出,我想将该 XML 输出显示为我的 Web 服务的输出。如果我有@Produces("application/xml")
它会起作用吗?
This is my code:
这是我的代码:
@Path("/inchi")
public class InChIto3D {
@GET
@Path("{inchiname}")
@Produces("application/xml")
public String get3DCoordinates(@PathParam("inchiname")String inchiName) {
String ne="";
try{
URL eutilsurl = new URL(
"http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?"
+ "db=pccompound&term=%22"+inchiName+"%22[inchi]");
BufferedReader in = new BufferedReader(
new InputStreamReader(eutilsurl.openStream()));
String inputline;
while ((inputline=in.readLine())!=null)
ne=ne+inputline;
}catch (MalformedURLException e1) {
}catch (IOException e2){
}
return ne;
}
}
回答by BalusC
The parameters should be URL encoded. You can use java.net.URLEncoder
for this.
参数应该是URL 编码的。您可以java.net.URLEncoder
为此使用。
String encodedParam = URLEncoder.encode(unencodedParam, "UTF-8");
The /
will then be translated to %2F
.
然后/
将被翻译成%2F
.
回答by Sashi Kiran Challa
I got this to work using @QueryParam() rather than @PathParam.
我使用@QueryParam() 而不是@PathParam 让它工作。
@Path("/inchi")
public class InChIto3D {
@GET
//@Path("{inchiname}")
@Produces("application/xml")
public String get3DCoordinates(@QueryParam("inchiname") String inchiName) {
String ne="";
try{
URL eutilsurl = new URL(
"http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?"
+ "db=pccompound&term=%22"+inchiName+"%22[inchi]");
BufferedReader in = new BufferedReader(
new InputStreamReader(eutilsurl.openStream()));
String inputline;
while ((inputline=in.readLine())!=null)
ne=ne+inputline;
}catch (MalformedURLException e1) {
}catch (IOException e2){
}
return ne;
}
}
Thus the URL structure would be like this http://myrestservice.com/rest/inchi?inchiname=InChIhere
因此 URL 结构将是这样的http://myrestservice.com/rest/inchi?inchiname=InChIhere
With @PathParam I read in the API that it will not accept slashes. I am wondering if I can use any Regular Expression in @Path just to ignore all slashes in the string that would be entered in quotes" ".
使用@PathParam,我在 API 中读到它不接受斜杠。我想知道是否可以在@Path 中使用任何正则表达式来忽略字符串中将在引号中输入的所有斜杠“”。
回答by simonox
Tomcat does not Accept %2F in URLs: http://tomcat.apache.org/security-6.html. You can turn off this behavior.
Tomcat 不接受 URL 中的 %2F:http: //tomcat.apache.org/security-6.html。您可以关闭此行为。
回答by yegor256
This is how you enable slashes in path params:
这是在路径参数中启用斜杠的方法:
@Path("{inchiname : .+}")
public String get3DCoordinates(@PathParam("inchiname")String inchiName)
回答by AmanicA
The following should work:
以下应该工作:
@GET
@Path("{inchiname : (.+)?}")
@Produces("application/xml")
public String get3DCoordinates(@PathParam("inchiname")String inchiName) {
(This is sort of mentioned in another answer and comment, I'm just explicitly putting it in a separate answer to make it clear)
(这是在另一个答案和评论中提到的,我只是明确地将它放在一个单独的答案中以使其清楚)