如何在WebServlet(HttpServlet)中使用PathParam指定扩展路径

时间:2016-03-30 17:35:28

标签: java rest servlets web.xml

我正在尝试编写一个REST servlet,它返回'/ connections'处的列表和'/ connections / {id}'处的项目,但GET请求只会转到第一个doGet()函数。我无法弄清楚如何编写web.xml来处理id字段。

我在这段代码中做错了什么?

@Path("connections")
public class Connections extends HttpServlet {

    public Connections() {
        super();
    }

    @GET
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //return list
    }

    @GET
    @Path("{id}")
    protected void doGet(HttpServletRequest request, HttpServletResponse response, @PathParam("id") String id)
            throws ServletException, IOException {
        //return item from list
    }
}

和web.xml

<web-app>
    <display-name>test</display-name>
    <servlet>
        <servlet-name>connections</servlet-name>
        <servlet-class>com.apiEndpoints.Connections</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>connections</servlet-name>
        <url-pattern>/connections</url-pattern>
        <url-pattern>/connections/*</url-pattern>
    </servlet-mapping>
</web-app>

0 个答案:

没有答案