[英]How exclude some paths from @GetMapping spring annotation?
我使用Spring Boot
並且我的主控制器處理所有頂級網址:
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class IndexController {
@GetMapping({"/*"})
public ResponseEntity<String> handleIndex() {
// ...
}
}
出於某些原因,我希望從處理我的控制器中排除路徑/exclude_path
。
像這樣的東西:
@GetMapping(include={"/*"}, exclude={"/exclude_path"})
你能回答我怎么做嗎?
首先, /*
是一種不好的做法。 如果您真的想這樣做,最好的選擇是為您的控制器引入一個方面。 並在那里過濾掉它。
像這樣的事情會做:
@Pointcut("@annotation(org.springframework.web.bind.annotation.RequestMapping)")
public void requestMapping() {}
@Pointcut("within(blah.blah.package.controller.*)")
public void myController() {}
@Around("requestMapping() && myController()")
public Object logAround(ProceedingJoinPoint joinPoint, RequestMapping requestMapping) throws Throwable {
String url = requestMapping.value();
// check the url and based on that do
// joinPoint.proceed();
}
您應該刪除“/*”,因為它會刪除所有請求。 只提供您想要的路徑,這樣您就不需要 /excludepath。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.