简体   繁体   中英

Jersey Servlet returning 405 even with GET and POST methods defined

I'm working with Jersey restful API with Atmosphere .

Here is my web.xml

    <servlet>
        <description>AtmosphereServlet</description>
        <servlet-name>AtmosphereServlet</servlet-name>
        <servlet-class>org.atmosphere.cpr.AtmosphereServlet</servlet-class>
        <init-param>
            <param-name>com.sun.jersey.config.property.packages</param-name>
            <param-value>com.example.p1</param-value>
        </init-param>
        <async-supported>true</async-supported>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet>
        <servlet-name>AdminPanel</servlet-name>
        <servlet-class>org.atmosphere.cpr.AtmosphereServlet</servlet-class>
        <init-param>
            <param-name>com.sun.jersey.config.property.packages</param-name>
            <param-value>com.example.p2</param-value>
        </init-param>
        <init-param>
            <param-name>com.sun.jersey.config.property.JSPTemplatesBasePath</param-name>
            <param-value>/WEB-INF/admin/</param-value>
        </init-param>
        <load-on-startup>2</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>AdminPanel</servlet-name>
        <url-pattern>/admin/*</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>AtmosphereServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

Now, I have a resource class with @Path("/register") and I have defined both @GET and @POST methods. There is no other class which handles the path /register in my entire project.

@Path("/register")
public class Register{
    @POST
    public String method1(){
        return "POST";
    }
    @GET
    public String method1(){
        return "GET";
    }
}

The url I'm using is http://129.123.3.3/user/register and I'm using the POST method in my call.

Inspite of defining both GET and POST methods I'm getting

HTTP Status 405 - Method Not Allowed

But, when I remove my serlvet mapping for AdminPanel and change the <url-pattern> for AtmosphereServlet to /* instead of / the appropriate method is called. But this way I cannot use URLS which are defined in my AdminPanel servlet. What am I missing? If this is a problem solely related to Servlet Mapping what can be the mapping so that the requests are correctly routed to /admin and the other requests are not hampered?

Implement a class

@javax.ws.rs.ApplicationPath("webresources")
public class ApplicationConfig extends javax.ws.rs.core.Application {
    @Override
    public Set<Class<?>> getClasses() {
        Set<Class<?>> resources = new java.util.HashSet<>();
        resources.add(Register.class);
        return resources;
    }
}

The your URL will be

GET /<context>/webresources/register

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