Spring @RequestMapping 路徑無法繼承

來源:互聯網
上載者:User

標籤:stat   style   get   closed   oca   技術   host   繼承   ram   

情境說明:在開發一個項目的時候,由於內部約定,必須在介面路徑上表明版本和項目,假設版本取v1, 項目:rms,那麼項目中所有介面路徑的設計出來後都是:http|https://ip:port|網域名稱/v1/rms/*;

       由於路徑中有/v1/rms共同的部分,所以我把它放在一個類中,該類用註解@RestController 標註表明它是一個controller 類,同時用註解@RequestMapping("/v1/rms")標註。那麼所有繼承該類的controller類的路徑都以“/v1/rms”作為路徑首碼。

1 import org.springframework.web.bind.annotation.RequestMapping;2 import org.springframework.web.bind.annotation.RestController;3 4 5 @RestController6 @RequestMapping("/v1/rms")7 public class RmsBaseController {8 9 }
View Code

      同時設計另外一個繼承它的類,該類繼承自RmsBaseController類,取名TestController,假設所有該類裡面定義的介面都以"/test"作為首碼,自然地,會想到和之前基類一樣用@RestController @RequestMapping("/test")註解該類,但是真的這樣做之後,發現程式能啟動,但是用 “http://ip:port/v1/rms/test/*” 訪問 該類的介面後會報404錯,而只能用 “http://ip:port/test/*”才能訪問。

錯誤碼

 1 import org.springframework.web.bind.annotation.RequestMapping; 2 import org.springframework.web.bind.annotation.RequestMethod; 3 import org.springframework.web.bind.annotation.RestController; 4 import javax.servlet.http.HttpServletResponse; 5  6 @RestController 7 @RequestMapping("/test") 8 public class TestController extends RmsBaseController { 9 10     /**11      * 添加測試介面Ping12      * */13     @RequestMapping(value = "ping", method = RequestMethod.GET)14     public final void checkApplication(HttpServletResponse response) {15         response.setStatus(200);16     }17 }
View Code

請求樣本:http://localhost:8090/vi/resource/test/ping

報錯內容:

<html><body><h1>Whitelabel Error Page</h1><p>This application has no explicit mapping for /error, so you are seeing this as a fallback.</p><div id=‘created‘>Wed Mar 07 19:38:20 CST 2018</div><div>There was an unexpected error (type=Not Found, status=404).</div><div>No message available</div></body></html>

修改方法

將TestController類註解@RequestMapping("/test")去掉,並且在TestController的每一個介面的@RequestMapping的value值前面加上首碼“/test”。改正後的代碼:

import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RestController;import javax.servlet.http.HttpServletResponse;@RestControllerpublic class TestController extends RmsBaseController {    /**     * 添加測試介面Ping     * */    @RequestMapping(value = "/test/ping", method = RequestMethod.GET)    public final void checkApplication(HttpServletResponse response) {        response.setStatus(200);    }}
View Code

測試樣本:http://localhost:8090/v1/rms/test/ping

結果:返回狀態代碼:200

可見 @RequestMapping("/test")不能同時注釋兩個有繼承關係的類。

記錄下踩過的坑,以便以後能繞過。

 

Spring @RequestMapping 路徑無法繼承

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.