2017-03-11 25 views
-1

我試圖找到它,但我發現了很多不同的場景,但沒有這個。如何在com.myproject.api下的所有控制器中添加「api」前綴?

我想要做的是在com.myproject.api下的控制器中的所有路由中添加「/ api /」前綴。 我想要「/ api/*」包下的所有控制器com.myapp.api和沒有前綴的所有控制器下com.myapp.web

Spring/Spring Boot可能嗎?

+0

這是不一樣的。 –

回答

0

已經回答了herehere

添加application.properties文件在src /主/資源,具有以下選項:

server.contextPath=/api 

檢查common properties的官方參考。

+0

我不想爲所有人添加基本網址。我正在用模板AND API創建應用程序,我想以前綴 –

+0

您希望com.myproject.api/**用於HTML視圖,而com.myproject.api/api/**用於其他控制器(例如REST控制器)? –

+0

不需要。我想爲包com.myapp.api下的所有控制器使用「/ api/*」,並且com.myapp.web的所有控制器都沒有前綴 –

0

您應該將@RequestMapping("/api")添加到每個想要的@Controller@RestController類別的頂部。

當類和方法都具有該註釋時,Spring Boot會在構建url時附加它們。在下面的例子中,該方法將被綁定到/api/user路由。

@RequestMapping("/api") 
@RestController 
public class MyController { 

    @RequestMapping("/user") 
    public List<User> getUsers(){ 
    return ... 
    } 

} 
+0

'位於每個需要的控制器或RestController類的頂部。 - 因此,配置Spring不是不可能的嗎?維護起來會更容易 –

+0

你說的是包級別註釋,但是Spring的'@ RequestMapping'不支持它。 – bekce

+0

不能作爲server.contextPath以某種方式完成,但只適用於包? –

相關問題