简体   繁体   English

Java Spring Boot-路由访问静态文件夹

[英]Java Spring Boot - routing for access to static folder

I am working on a reactjs project and I just did a yarn build and moved the contents into the Java project. 我正在做一个reactjs项目,我只是做了一个yarn并将内容移到Java项目中。

my-project\\complete\\src\\main\\resources\\static 我的项目\\完成\\ SRC \\主\\资源\\静态

but when I view the project from the Java site -- localhost:8080 -- I get a ton of 404 errors 但是当我从Java站点-localhost:8080查看项目时-我收到大量404错误

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? 我的Application.java文件看起来像这样-当我取消注释代码时,我被告知要删除@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? 我试图添加一个配置文件来解决这个问题。是否需要将其导入到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/");
    }

}

我想你缺少@WebMvcAutoConfiguration批注

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM