简体   繁体   中英

Catch all requested paths in Springboot RestController

I am looking for a way to get an Endpoint in Springboot that catches all requests send to / . Ideally everything behind / should be handed in as a String parameter.

An example request could look like this: http://myproxy.com/foo/bar?blah=blubb I tried a RestController for /

@RestController
public class ProxyRestController {

    @RequestMapping("/{restOfPath}", method = RequestMethod.GET)
    public ResponseEntity<String> handleGetRequests(@PathVarialbe("restOfPath") String path) {
        return ResponseEntity.of(Optional.of(""));
    }

}

The endpoint doesn't catch the example because it would be routed to /foo/bar whereas /foo is caught.

How would I achieve a "catch all" endpoint in SpringBoot? It could also be in another way than a @RestController I just need to be inside a component and send a http response back to the caller.

Adapt this code to match yours:

@Controller
public class RestController {

@RequestMapping(value = "/**/{path:.*}")
public String index(final HttpServletRequest request) {
    final String url = request.getRequestURI();

    return "something";
}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM