2

I have a spring boot app and a controller that server static webpage(React build):

@Controller
@RequestMapping("/test")
public class HomeController {

    @GetMapping("/")
    public String index() {
    return "index.html";
} 
 ...

index.html is located at: ../resources/static/index.html

also in application.yml:

spring:
  mvc:
    static-path-pattern: /test/**

I am having two problems(problem 2 is the main issue):

  1. I must call the following url with the trailing '/' at the end: http://localhost:8100/test/ I would like for http://localhost:8100/test to also map me to the view(index.html).

  2. during the load of the page I am getting the following error: Error

the problem as you can see is that the url called is: http://localhost:8100/static/css/main.6c417d20.chunk.css

and not

http://localhost:8100/test/static/css/main.6c417d20.chunk.css

(please note that the reason for the 'static' in the url is that there is a folder named: static below the resources/static folder so there is no issue with the 'static' in the url)

is it a server side problem or is it something I should fix in the react?

I searched for an answer but didn't find anything helpful.

any help would be highly appreciated, Tnx

Roie Beck
  • 1,113
  • 3
  • 15
  • 30

2 Answers2

3

So the answer to my question lies in the following links:

  1. how to build react to a non root path(homepage):

    build react non root path

  2. registering zuul client to the following path(that contains all resources):

    Zuul configuration with resources

so I am leaving this here in case someone has the same issue(the answer in the second link is for vue.js and webpack,the first link explains how to change root address in react).

Roie Beck
  • 1,113
  • 3
  • 15
  • 30
0

Answer 1 : @RequestMapping has a String[] value parameter, so can specify multiple values like this:

@RequestMapping(value={"", "/", "welcome"})

Answer 2 : You are expecting test in URL which is controller mapping not the project context path so it should not come in static resources urls.

see this answer for more clarity adding css and js in spring boot.

Alien
  • 15,141
  • 6
  • 37
  • 57
  • many thanks, I am not using thymeleaf as in the answer, can you peovide a small example?should I edit context path parameter? – Roie Beck Nov 06 '18 at 05:04
  • I want the static resources to be mapped under /test. That is important to me because I am behind zuul proxy, so I cannot be placed under root, how can I map all my static context under test? And create the contoller accordingly? – Roie Beck Nov 06 '18 at 13:10
  • thanks for all the help, I managed to solve all my problems , but it was me not explaining the problem currently – Roie Beck Nov 07 '18 at 15:18
  • Cool...happy coding – Alien Nov 07 '18 at 15:26