Search code examples
javaspring-bootspring-restcontroller

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.


Solution

  • 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";
    }
    }