-1

Have a spring boot project and a default controller:

@Controller
public class GenericController
{
    @RequestMapping(value= {"/**.html", "/"})
    public String httpRequest(Model model, HttpServletRequest request)
    {

But works only with /*.html routes. How to catch all .html routes with any folder source? example: /abc.html, /abc/def.html, /abc/def/ghi.html, etc.

I learn about:

And try with:

@RequestMapping(value= {"/**/*.html", "/"})

But does not works, when call http://localhost/abc/def/ghi.html returns an http status 404.

e-info128
  • 3,727
  • 10
  • 40
  • 57

2 Answers2

0

I don't know why you want to do that but you can hack path params to do it for you. But its a dirty way and can cause conflicts with other mappings.

By using path params like below you can do /abc.html, /abc/def.html, /abc/def/ghi.html.

@RequestMapping(value = { "/**.html" , "/{path2}/**.html" ,"/{path}/{path2}/**.html" })
public String httpRequest(Model model) {
    //You can also check which path variables are present and work accordingly 
    System.out.println("index");
    return "index";
}

If you want to create a single entry point for your API then I would suggest you to read about GraphQL

UsamaAmjad
  • 4,175
  • 3
  • 28
  • 35
0

Another approach can be using a Filter, that redirects your response according to incoming URI:

@Component
@Order(1)
public class AFilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

        HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
        if(httpServletRequest.getRequestURI()...){ // use regex ?
            HttpServletResponse httpServletResponse = (HttpServletResponse) servletResponse;

            ((HttpServletResponse) servletResponse).sendRedirect("/some/path/to/your/thingy");
        }

        filterChain.doFilter(servletRequest, servletResponse);
    }
}

And some controller:

@RequestMapping(value = "/some/path/to/your/thingy", method = RequestMethod.GET)
public ResponseEntity<Object> aMethod() throws Exception {

    return ResponseEntity.ok("ok");
}
oldborn
  • 86
  • 2