spring boot不启动静态Web内容

时间:2014-03-28 17:56:50

标签: spring-boot

我试图在我的春季启动应用中启动index.html,但请参阅404.我错过了什么依赖?

build.gradle(多项目)

project('sub-project')
{
apply plugin: 'spring-boot'
compile (
    "org.springframework.boot:spring-boot-starter-web:1.0.0.RC5",
    "org.springframework.boot:spring-boot-starter-actuator:1.0.0.RC5"
.. few more app specific dependencies
)

项目结构:

MainProject
  -- sub-project
     src
       main
          resources
             index.html

申请类:

@Configuration
@EnableAutoConfiguration
class Application {
    public static void main(String[] args) {
        SpringApplication.run([SpringServlet, Application, "classpath:/META-INF/com/my/package/bootstrap.xml"] as Object[], args)
    }
}

**Launching http://localhost:8080/index.html throws 404.**

2 个答案:

答案 0 :(得分:2)

找到根本原因。将SpringServlet的Url映射更改为" Rest"资源特定路径修复它。 早些时候" / *"也被SpringServlet解释,无法呈现index.html。

class Application extends SpringBootServletInitializer {
    public static void main(String[] args) {
        SpringApplication.run([Application, "classpath:/META-INF/com/my/package/mgmt/bootstrap.xml"] as Object[], args)
    }

    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application);
    }

    @Bean
    ServletRegistrationBean jerseyServlet() {
        ServletRegistrationBean registration = new ServletRegistrationBean(new SpringServlet(), "/rest/*");
        Map<String, String> params = ["com.sun.jersey.config.property.packages": "com.my.package.mgmt.impl;com.wordnik.swagger.jersey.listing"]
        registration.setInitParameters(params)
        return registration;
    }

    @Bean
    ServletRegistrationBean jerseyJaxrsConfig() {
        ServletRegistrationBean registration = new ServletRegistrationBean(new DefaultJaxrsConfig(), "/api/*");
        Map<String, String> params = ["swagger.api.basepath": "http://localhost:8080/api", "api.version": "0.1.0"]
        registration.setInitParameters(params)
        return registration;
    }

答案 1 :(得分:1)

@Configuration
public class WebConfig implements WebMvcConfigurer {

/** do not interpret .123 extension as a lotus spreadsheet */
    @Override
    public void configureContentNegotiation(ContentNegotiationConfigurer configurer) 
    {
        configurer.favorPathExtension(false);
    }

/**
  ./resources/public is not working without this
*/
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/**")
            .addResourceLocations("classpath:/public/");
}

}