為Servlet添加支持REST式URL:ServletREST
為Servlet添加支持REST式URL: 不改變習慣,僅僅在servlet內部完成doGet, doPost,doDelete,doPut等方法即可映射到較為復雜的REST URL
eg:
/book/head first java/ 對應的URL表達式為: /book/*/ /book/head first java/chapter/1 對應的URL表達式為: /book/*/chapter/*
您所要做的: 僅僅需要在web.xml中配置一個filter 僅僅需要在集成HttpServlet添加一個注解(eg:@RestSupport("/book/*/chapter/*"))
下面附加一個使用示范:
@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 ... } }
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!