Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 472
Tom Avatar asked Oct 23 '25 04:10

Tom


1 Answers

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";
}
}
like image 151
Ebenezer Nikabou Avatar answered Oct 25 '25 17:10

Ebenezer Nikabou



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!