JAX-RS @PathParam 如何传递带有斜杠、连字符和符号的字符串也等于
我是 JAX-RS 的新手,我正在尝试使用 Jersey 构建一个简单的 RESTful Web 服务。
我有 2 个问题。请澄清这些:
我正在尝试让我的简单网络服务像这样的 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)
。我如何将其作为@PathParam
传递,我的意思是普通字符串工作正常,但这里有斜杠、连字符和逗号。我该如何让它忽略这些。我尝试将其放在引号中,但这不起作用。我应该怎么做?我需要将该
InChI
传递给另一个 Web 服务,并返回一个 XML 作为输出,并且我希望将该 XML 输出显示为我的 Web 服务的输出。如果我有@Produces("application/xml")
它会工作吗?
这是我的代码:
@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;
}
}
I am new to JAX-RS and I am trying to use Jersey to build a simple RESTful Webservice.
I have 2 questions. Please clarify these:
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?
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;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这是在路径参数中启用斜杠的方法:
This is how you enable slashes in path params:
Tomcat 不接受 URL 中的 %2F:http://tomcat.apache.org/security-6。 html。您可以关闭此行为。
Tomcat does not Accept %2F in URLs: http://tomcat.apache.org/security-6.html. You can turn off this behavior.
以下内容应该有效:
(这在另一个答案和评论中有所提及,我只是明确地将其放在单独的答案中以使其清楚)
The following should work:
(This is sort of mentioned in another answer and comment, I'm just explicitly putting it in a separate answer to make it clear)
参数应为URL 编码。您可以使用
java.net.URLEncoder
为此。
然后,
/
将被转换为%2F
。The parameters should be URL encoded. You can use
java.net.URLEncoder
for this.The
/
will then be translated to%2F
.我使用 @QueryParam() 而不是 @PathParam 来实现它。
因此,URL 结构将类似于 http://myrestservice.com/rest/inchi?inchiname= InChIhere
使用 @PathParam,我在 API 中读到它不接受斜杠。我想知道是否可以在 @Path 中使用任何正则表达式来忽略在引号“”中输入的字符串中的所有斜杠。
I got this to work using @QueryParam() rather than @PathParam.
Thus the URL structure would be like this 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" ".