简体   繁体   中英

Jersey REST webservice subresources not found

At first in my web server I only had one 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:

 <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/*

and the @Path directive of Controller servlet class from "/" to "/rest". Everything works fine but the sub-resources or methods of controller with the @path directive that stopped working.. ie:

  • / works fine since I have an index.html page at root
  • /rest works fine, it invokes the get method of my servlet
  • /rest/resource1 returns 404 http code...

Any help? I already tried a list of combinations of / after and before each @Path directive, with no success... Many thanks

One update:

I used the trace util and got the following results:

for /[app-name]/rest (it works):

  • X-Jersey-Trace-002 accept right hand path java.util.regex.Matcher[pattern=/rest(/.*)? region=0,11 lastmatch=/rest]: "/rest" -> "/rest" : ""
  • X-Jersey-Trace-003 accept resource: "rest" -> @Path("/rest") xpto.mypack.Controller
  • X-Jersey-Trace-000 accept root resource classes: "/rest"
  • X-Jersey-Trace-001 match path "/rest" -> "/application.wadl(/. )?", "/rest(/. )?"

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

  • X-Jersey-Trace-002 matched exception mapper: 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(/. )?"

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. Ie all you need to do is keep the controller as is (in your question above) and just change the servlet mapping. The URL at which the resources are available is:

<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:

<application_context_path>/rest/rest/*

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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