开发者

RESTEasy - @Path requiring a full path?

开发者 https://www.devze.com 2023-01-23 23:17 出处:网络
I was messing around with JAX-RS and made an application which calls REST services which produce JSON. I tried Jersey and everything went fine, but I had to switch to RESTEasy as my application needs

I was messing around with JAX-RS and made an application which calls REST services which produce JSON. I tried Jersey and everything went fine, but I had to switch to RESTEasy as my application needs to be built with JDK5. I changed my web.xml to something like this:

<web-app>
<context-param>
    <param-name>resteasy.scan</param-name>
    <param-value>true</param-value>
</context-param>

<listener>
    <listener-class>
     org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap
    </listener-class>
</listener>

<servlet>
    <servlet-name>RESTEasy</servlet-name>
    <servlet-class>org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>RESTEasy</servlet-name>
    <url-pattern>/rest/*</url-pattern>
</servlet-mapping>
<!-- ... -->
</web-app>

So I expect every URL starting with /rest to be handled by RESTEasy. My service开发者_如何学Cs are as follows:

@Path("/services")
public class MyRESTServices {

    @GET
    @Path("service1")
    @Produces(MediaType.APPLICATION_JSON)
    public Object service1(Blah blah) {

    }
}

This worked fine using Jersey, http://localhost/MyContext/rest/services/service1 was bound to my service1() method. When I change to RESTEasy, though, I had a 404:

HTTP Status 404 - Could not find resource for relative : /rest/services/service1 of full path: http://localhost/MyContext/rest/services/service1

Which means that RESTEasy handled the request but could not find any service bound to this URL.

On my class, changing @Path("/services") to @Path("/rest/services") worked, though. Do you have any idea why I got this strange behaviour? All the tutorials/docs I read mentionned only relative paths, not including the /rest prefix...


Solution: add the following in your web.xml

<context-param>
    <param-name>resteasy.servlet.mapping.prefix</param-name>
    <param-value>/rest</param-value>
</context-param>

Where /rest is the beginning of your <url-pattern>/rest/*</url-pattern>

(Source: http://docs.jboss.org/resteasy/docs/2.0.0.GA/userguide/html/Installation_Configuration.html#d0e72)


On JBoss AS 7.1 I also had to add to add resteasy.resources ... which is further explained here http://www.javaroots.com/2013/05/creating-rest-services-with-rest-easy.html You may get error like this : Could not find resource for relative : /application/test of full path:... You have to define resteasy.resource context param with the full path of Rest class.

0

精彩评论

暂无评论...
验证码 换一张
取 消