jonashackt/spring-boot-vuejs

404 - Not found when entered directly into the browser

vikrun opened this issue ยท 15 comments

When I enter the main page it works fine and go into another page it works fine.
But if I enter the another webpage in the url, it dont works. I get a 404 from Spring.
The problem is on the server side. I need to configure this.

I am building a Vue.JS & Spring Boot app which I am running through a docker container. The dist folder for Vue is copied to resources/public path and served through the Spring Boot service.

I have set up routes using vue router, but all of these routes return 404 - Not found when entered directly into the browser (but work fine when accesses through the Vue app).

What is the best solution to fix this? Should I create a .htacccess file?

Never mind. I had to create a redirect controller.

/* Redirects all routes to FrontEnd except: '/', '/index.html', '/api', '/api/**' /
@RequestMapping(value = "{_:^(?!index\.html|api).
$}")
public String redirectApi() {
return "forward:/";
}

I think this should be reopened and added to the sample code!

@vikrun @renanleandrof You're absolutely right - since we use mode: history (see Vue.js docs) now, we also need to forward every other request to the frontend again. Fixing this soon.

Should be fixed now, please have a look @vikrun @renanleandrof :octocat:

I tried it today and the redirect is not working, still showing the 404 using current version of master. How should this be fixed?

Yeah, same here. Tried that and got 404:

https://spring-boot-vuejs.herokuapp.com/callservice

=/

try to add another Controller
package de.jonashackt.springbootvuejs.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

@controller
public class OtherController {

@GetMapping(value = "{_:^(?!index\.html|api).$}")
public String entry() {
return "index";
}

}

I tried adding that other controller, still no luck, got 404

something that worked for me:

package de.jonashackt.springbootvuejs.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class OtherController {
    @RequestMapping("{?:(?:(?!api|static|\\.).)*}/**")
    public String redirectApi() {     
        return "/index.html";
    } 
}

@jonashackt This issue should be fixed by #79

Looks still not working. It looks the fix #79 didn't work because it's attached to the requestmapping /api
Anything outside /api will return a 404. I think manage this on a separate controller looks better like @velaan suggest it

I am able to redirect to the home page , but still haven't figured out a way to give control back to vue controller.

I think the current code only works for single-level request forwarding. For example:
/videos would work but /videos/5 would not.

Was able to get the second level working using:

@RequestMapping("{?:(?:(?!api|static|\\.).)*}/**")

but is there a way to not have to keep doing /** to continuously support more levels?

@AdrianLeeElder have you checked #92? It should fix this issue for good.

@AdrianLeeElder have you checked #92? It should fix this issue for good.

That seems to be working perfectly! A small optimization would be to move the pattern into a static field so we're not recompiling the pattern on each request.