ServletREST為Servlet添加支持REST式URL
為Servlet添加支持REST式URL: 不改變習(xí)慣,僅僅在servlet內(nèi)部完成doGet, doPost,doDelete,doPut等方法即可映射到較為復(fù)雜的REST URL
eg:
/book/head first java/
對(duì)應(yīng)的URL表達(dá)式為:
/book/*/
/book/head first java/chapter/1
對(duì)應(yīng)的URL表達(dá)式為:
/book/*/chapter/*
您所要做的: 僅僅需要在web.xml中配置一個(gè)filter 僅僅需要在集成HttpServlet添加一個(gè)注解(eg:@RestSupport("/book/*/chapter/*"))
下面附加一個(gè)使用示范:
@RestSupport("/book/*/chapter/*")
public class ChapterServlet extends HttpServlet {
private static final long serialVersionUID = -1534235656L;
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// code here ...
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// code here ...
}
protected void doPut(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// code here ...
}
protected void doDelete(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// code here ...
}
}
評(píng)論
圖片
表情
