尝试获取时,请求的资源不可用

时间:2016-09-12 19:11:28

标签: java apache web

我想让这个网址正常工作

http://localhost:8080/HomeController/students/getAllStudents

我收到了这个错误:

  

请求的资源不可用

这是我的代码:

package controller;
    @Path("/students")
    public class HomeController {

        @Path("/add")
        @POST
        @Produces(MediaType.TEXT_PLAIN)
        @Consumes(MediaType.APPLICATION_JSON)
        public static String add(Student student) throws Exception {

        @Path("/getAllStudents") 
        @GET 
        @Produces(MediaType.TEXT_PLAIN) 
        public String getAllStudents() throws Exception {

            return "dasda";
        }

    }
}

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    id="WebApp_ID" version="3.0">
    <display-name>StudentsService</display-name>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
    </welcome-file-list>

<servlet>
    <servlet-name>
        Jersey REST Service
    </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>
            controller
        </param-value>

    </init-param>

    <init-param>

        <param-name>
            com.sun.jersey.api.json.POJOMappingFeature
        </param-name>

        <param-value>true</param-value>

    </init-param>

    <load-on-startup>1</load-on-startup>

</servlet>

<servlet-mapping>

    <servlet-name>Jersey REST Service</servlet-name>

    <url-pattern>/HomeController/*</url-pattern>

</servlet-mapping>

</web-app> 

在第一个链接起作用后,我希望此链接也能正常工作。

http://localhost:8080/students/getAllStudents

1 个答案:

答案 0 :(得分:0)

     @Path("/students")
    public class HomeController {

        @Path("/add")
        @POST
        @Produces(MediaType.TEXT_PLAIN)
        @Consumes(MediaType.APPLICATION_JSON)
        public static String add(Student student) throws Exception {

        @Path("/getAllStudents") 
        @GET 
        @Produces(MediaType.TEXT_PLAIN) 
        public String getAllStudents() throws Exception {

            return "dasda";
        }

    }
}

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    id="WebApp_ID" version="3.0">
    <display-name>StudentsService</display-name>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
    </welcome-file-list>

<servlet>
    <servlet-name>
        Jersey_REST_Service
    </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>
            name_of_teh package(e.g. if your HomeController lies in com.urName.controller.HomeController, then value is com.urName.controller)
        </param-value>

    </init-param>

    <init-param>

        <param-name>
            com.sun.jersey.api.json.POJOMappingFeature
        </param-name>

        <param-value>true</param-value>

    </init-param>

    <load-on-startup>1</load-on-startup>

</servlet>

<servlet-mapping>

    <servlet-name>Jersey_REST_Service</servlet-name>

    <url-pattern>/HomeController/*</url-pattern>

</servlet-mapping>

</web-app> 

@Path应该包含从不在结尾处使用斜杠,正弦表示路径类似于/ students //。 此外,Controller包尚未与Jersey软件包一起映射,它应该在其中找到控制器。 请尝试上面发布的更正的代码段。