问题描述
期望拦截以 /test 开头的任意 URL,并且获取后面完整的 URL。
比如有一个 URL:/test/update/user/2
期望:拦截该请求,并且获取到 URL 后缀 /update/user/2
代码实现
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/test")
public class TestController {
@RequestMapping(value = "/{*requestParam}")
public String test2(@PathVariable String requestParam) {
return String.format("Hello %s! ", requestParam);
}
}
启动类:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringBootDemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootDemoApplication.class, args);
}
}
测试
浏览器访问:http://localhost:8080/test/hello/aa
,可以得到返回值输出:Hello /hello/aa!
,由此获取到了预期的 URL:/hello/aa。