Java 如何从@GetMapping spring注释中排除某些路径?
我使用Java 如何从@GetMapping spring注释中排除某些路径?,java,spring,spring-mvc,spring-boot,Java,Spring,Spring Mvc,Spring Boot,我使用Spring Boot,我的主控制器处理所有顶级URL: import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; @Controller public class IndexController { @GetMapping({
Spring Boot
,我的主控制器处理所有顶级URL:
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() {
// ...
}
}
你能回答我怎么做吗?你应该删除“/*”,除了所有的请求。只提供您想要的路径,这样您就不需要/excludepath了。首先,
/*
是一种不好的做法。如果您想这样做,最好的选择是为控制器引入一个方面。然后将其过滤掉。这样做可以:
@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();
}
如果不将其放在GetMapping中,它将不会被处理。不确定这里有什么问题。@pvpkiran,我的路径中有通配符
/*
,只想排除一个映射到此通配符的url(/exclude\u path)。我有单页应用程序,不管理前端。如果我删除/*
,我将不得不在代码中写入每个新的url。
@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();
}