0

I am working on a reactjs project and I just did a yarn build and moved the contents into the Java project.

my-project\complete\src\main\resources\static

but when I view the project from the Java site -- localhost:8080 -- I get a ton of 404 errors

logo.a67f8998.png:1 GET http://localhost:8080/static/media/logo.a67f8998.png
registerServiceWorker.js:77 GET http://localhost:8080/service-worker.js 404
-beefeater.78e4414b.jpg:1 GET http://localhost:8080/static/media/-beefeater.78e4414b.jpg 404 ()

For this project I am mostly acting as a frontend dev - Where do I make the required changes to get the routing correct for these files?

my Application.java file looks like this - when I uncomment the code I get told to remove the @overide?

//import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;


/**
 * Hello world!
 *
 */
@SpringBootApplication
public class Application implements CommandLineRunner {

    //@Autowired
    //private AccountRepository accountRepository;

    /**
     * @param args
     */
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Override
    public void run(String... arg0) throws Exception {

    }

    /*
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
      registry.addResourceHandler("/static/**")
        .addResourceLocations("classpath:/static/");
    }*/

}

I've tried to add a config file to sort this out.. do I need to import it in the application.java?

import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

//@Configuration
@EnableWebMvc
@WebAppConfiguration
public class Configuration extends WebMvcConfigurerAdapter {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry
          .addResourceHandler("/static/**")
          .addResourceLocations("classpath:/static/");
    }

}
The Old County
  • 89
  • 13
  • 59
  • 129
  • Since your are using `@SpringBootApplication` it is not necessary to configure the resource handler because spring boot will do that for you. Just make sure that the static conent is under src/main/resources/static for example `src/main/resources/static/myscript.js` then if you need to consume it `localhost:8080/myscript.js` and make sure to remove `@EnableWebMvc`. – Daniel C. Sep 08 '17 at 23:54
  • I don't understand -- its located here "\src\main\resources\static" -- but I get 404's - your saying I don't need to configure it - but why am I getting 404's -- "http://localhost:8080/service-worker.js 404" – The Old County Sep 08 '17 at 23:56
  • `@EnableWebMvc` will disable the spring boot auto-configuration for static web content, if you use `@EnableWebMvc` then you are forced to configure the resource handler. – Daniel C. Sep 09 '17 at 00:00
  • @DanielC. -- ok -- I have something -- my project doesn't sit on the pure root per say -- but "http://localhost:8080/mywebsite/service-worker.js" -- so the reactjs build is looking for the files on "http://localhost:8080/service-worker.js" --- what to do? – The Old County Sep 09 '17 at 00:01
  • @DanielC. -- in my application.settings should I just have " server.contextPath= /mywebsite" set to " server.contextPath= /" – The Old County Sep 09 '17 at 00:02
  • please lets continue in chat https://chat.stackoverflow.com/rooms/153992/java-spring-boot-routing-for-access-to-static-folder – Daniel C. Sep 09 '17 at 00:04
  • @DanielC. I think I fixed it by making everything sit on the root - changed my server.context -- now I can gain access to the static folder – The Old County Sep 09 '17 at 00:24
  • @the-old-contry please share your solution as an answer it will help other people with similar situation, please share all the details. Also if you need to change context path please check this post https://stackoverflow.com/questions/20405474/add-context-path-to-spring-boot-application#20418450 – Daniel C. Sep 09 '17 at 00:28
  • @DanielC. -- sure - I do have a bug for sending emails with images -- I am interested in gaining access to images for the email templates that would get sent out.. "complete\src\main\resources\static\images" seems inaccessible via ""localhost:8080/images" -- whereas the contents of the react static is available -- "complete\src\main\resources\static\static" -- ""localhost:8080/static" – The Old County Sep 09 '17 at 00:33
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/153996/discussion-between-the-old-county-and-daniel-c). – The Old County Sep 09 '17 at 01:54

1 Answers1

0

You're missing @WebMvcAutoConfiguration annotation I think

Zilvinas
  • 5,518
  • 3
  • 26
  • 26