简体   繁体   English

找不到Jersey REST Web服务子资源

[英]Jersey REST webservice subresources not found

At first in my web server I only had one REST servlet. 最初,在Web服务器中,我只有一个REST Servlet。 Something like: 就像是:

@Path("/")
public class Controller {
  @GET
  @Produces({ MediaType.TEXT_HTML })
  public Response get(@Context UriInfo info) throws Exception {
    ...
  }

  @GET
  @Path("resource1")
  @Produces({ MediaType.TEXT_HTML })
  public Response resource1() throws Exception {
    ...
  }

  ...

 }

And the web.xml: 和web.xml:

 <servlet>
    <servlet-name>rest</servlet-name>
    <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>com.sun.jersey.config.property.packages</param-name>
        <param-value>xpto.mypack1;xpto.mypack2</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>rest</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

But then I wanted to add some static html to the server, so I updated the servlet mapping to /rest/* 但是后来我想向服务器添加一些静态html,所以我将servlet映射更新为/ rest / *

and the @Path directive of Controller servlet class from "/" to "/rest". 以及Controller Servlet类的@Path指令从“ /”到“ / rest”。 Everything works fine but the sub-resources or methods of controller with the @path directive that stopped working.. ie: 一切正常,但是带有@path指令的控制器的子资源或方法已停止工作。即:

  • / works fine since I have an index.html page at root /正常运行,因为我在根目录下有一个index.html页面
  • /rest works fine, it invokes the get method of my servlet / rest正常工作,它调用我的servlet的get方法
  • /rest/resource1 returns 404 http code... / rest / resource1返回404 HTTP代码...

Any help? 有什么帮助吗? I already tried a list of combinations of / after and before each @Path directive, with no success... Many thanks 我已经尝试了每个@Path指令前后的/组合列表,但没有成功...非常感谢

One update: 一个更新:

I used the trace util and got the following results: 我使用了跟踪工具,并得到了以下结果:

for /[app-name]/rest (it works): 对于/ [app-name] / rest(有效):

  • X-Jersey-Trace-002 accept right hand path java.util.regex.Matcher[pattern=/rest(/.*)? X-Jersey-Trace-002接受右手路径java.util.regex.Matcher [pattern = / rest(/.*)? region=0,11 lastmatch=/rest]: "/rest" -> "/rest" : "" region = 0,11 lastmatch = / rest]:“ / rest”->“ / rest”:“”
  • X-Jersey-Trace-003 accept resource: "rest" -> @Path("/rest") xpto.mypack.Controller X-Jersey-Trace-003接受资源:“ rest”-> @Path(“ / rest”)xpto.mypack.Controller
  • X-Jersey-Trace-000 accept root resource classes: "/rest" X-Jersey-Trace-000接受根资源类:“ / rest”
  • X-Jersey-Trace-001 match path "/rest" -> "/application.wadl(/. )?", "/rest(/. )?" X-Jersey-Trace-001匹配路径“ / rest”->“ /application.wadl(/。 )?”,“ / rest(/。 )?”

for /[app-name]/rest/resource1 (it doesn't work): 对于/ [app-name] / rest / resource1(无效):

  • X-Jersey-Trace-002 matched exception mapper: com.sun.jersey.api.NotFoundException@4fd41dc3 -> xpto.myclass X-Jersey-Trace-002匹配的异常映射器:com.sun.jersey.api.NotFoundException@4fd41dc3-> xpto.myclass
  • X-Jersey-Trace-003 mapped exception to response: com.sun.jersey.api.NotFoundException@4fd41dc3 -> 404 (Not Found) X-Jersey-Trace-000 accept root resource classes: "/resource1" X-Jersey-Trace-001 match path "/resource1" -> "/application.wadl(/. )?", "/rest(/. )?" X-Jersey-Trace-003映射到响应的异常:com.sun.jersey.api.NotFoundException@4fd41dc3-> 404(未找到)X-Jersey-Trace-000接受根资源类:“ / resource1” X-Jersey- Trace-001匹配路径“ / resource1”->“ /application.wadl(/。 )?”,“ / rest(/。 )?”

I hope it helps someone to help me.. 我希望它能帮助某人帮助我。

If you define your servlet mapping as /rest/* , don't repeat /rest in the @Path annotation of your resources. 如果将servlet映射定义为/rest/* ,请不要在资源的@Path注释中重复/rest Ie all you need to do is keep the controller as is (in your question above) and just change the servlet mapping. 也就是说,您需要做的就是保持控制器不变(在上面的问题中),然后更改servlet映射。 The URL at which the resources are available is: 资源可用的URL是:

<application_context_path>/<servlet_mapping>

So, if you change the @Path annotation from @Path("/") to @Path("rest") and you also change the servlet mapping to /rest , then your resources would be available at: 因此,如果将@Path注释从@Path("/")更改为@Path("rest")并且还将servlet映射更改为/rest ,则可以在以下位置获得资源:

<application_context_path>/rest/rest/*

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM