2017-08-09 103 views
0

我有一個springboot應用程序,在本地運行時爲網頁提供服務,但由於某種原因返回404時,我將部署war文件打包爲生產環境,出現404錯誤。在我的控制器中,我返回一個包含來自我的/ resources/static文件夾的html頁面的jsp頁面。這一切都在當地正常工作。當部署到生產時,Springboot應用程序網頁返回404

我部署到tomcat的7.0.52

這是我的聚甲醛

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
<modelVersion>4.0.0</modelVersion> 
<groupId>demo</groupId> 
<artifactId>demo</artifactId> 
<version>0.0.1-SNAPSHOT</version> 
<packaging>war</packaging> 

<properties> 
    <spring.version>4.3.9.RELEASE</spring.version> 
    <java.version>1.8</java.version> 
    <maven-compiler.version>3.6.1</maven-compiler.version> 
</properties> 
<parent> 
    <groupId>org.springframework.boot</groupId> 
    <artifactId>spring-boot-starter-parent</artifactId> 
    <version>1.5.4.RELEASE</version> 
    <relativePath /> <!-- lookup parent from repository --> 
</parent> 
<dependencies> 
    <!-- //////////////////////////////////////////////////////// --> 
    <!-- SPRING --> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-context</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-webmvc</artifactId> 
    </dependency> 

    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-web</artifactId> 
    </dependency> 

    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-tomcat</artifactId> 
     <scope>provided</scope> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-test</artifactId> 
     <scope>test</scope> 
    </dependency> 
    <dependency> 
     <groupId>org.apache.tomcat</groupId> 
     <artifactId>tomcat-jdbc</artifactId> 
     <version>7.0.19</version> 
     <scope>provided</scope> 
    </dependency> 
    <dependency> 
     <groupId>org.apache.tomcat.embed</groupId> 
     <artifactId>tomcat-embed-jasper</artifactId> 
     <scope>provided</scope> 
    </dependency> 
    <dependency> 
     <groupId>javax.servlet</groupId> 
     <artifactId>jstl</artifactId> 
    </dependency> 
</dependencies> 

<build> 
    <plugins> 
     <plugin> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-maven-plugin</artifactId> 
      <executions> 
       <execution> 
        <goals> 
         <goal> 
          repackage 
         </goal> 
        </goals> 
       </execution> 
      </executions> 
     </plugin> 
     <plugin> 
      <groupId>org.apache.tomcat.maven</groupId> 
      <artifactId>tomcat7-maven-plugin</artifactId> 
      <version>2.2</version> 
     </plugin> 
     <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-compiler-plugin</artifactId> 
      <configuration> 
       <source>${java.version}</source> 
       <target>${java.version}</target> 
      </configuration> 
     </plugin> 
    </plugins> 
</build> 

`

我Springboot應用程序的主類

@SpringBootApplication 
public class SpringbootApplication extends SpringBootServletInitializer { 

@Override 
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { 
    return application.sources(SpringbootApplication.class); 
} 

public static void main(String[] args) throws Exception { 
    SpringApplication.run(SpringbootApplication.class, args); 
} 
} 

我的類擴展webmvcConfigureAdapter

@Configuration 
@EnableWebMvc 
public class MvcConfiguration extends WebMvcConfigurerAdapter{ 

@Override 
public void addViewControllers(ViewControllerRegistry registry) { 
    System.out.println("in mvc config"); 
    registry.addViewController("/").setViewName("forward:index2.html"); 
} 

@Override 
public void configureDefaultServletHandling(
     DefaultServletHandlerConfigurer configurer) { 
    System.out.println("configure serve handling"); 
    configurer.enable(); 
} 

@Bean 
InternalResourceViewResolver internalResourceViewResolver() { 
    InternalResourceViewResolver viewResolver = new InternalResourceViewResolver(); 
    viewResolver.setPrefix("/WEB-INF/jsp/"); 
    viewResolver.setSuffix(".jsp"); 
    return viewResolver; 
} 
} 

我的主控制器。

@Controller 
public class MainController { 
    @RequestMapping(value={"/"}, method = RequestMethod.GET) 
    public String index(){ 
     System.out.println("inside mainController"); 
     return "chatbot"; 
    } 
} 

最後我的application.properties文件看起來像這樣。

spring.mvc.view.prefix: /WEB-INF/jsp/ 
spring.mvc.view.suffix: .jsp 

我的項目結構看起來像這樣...

src/main/webapp 
       -> assets 
       -> resources 
        -> static ->index.html 
       -> WEB-INF  
        ->jsp -> index.jsp 

請幫助。我一直堅持8小時以上。 謝謝。

+0

你想要達到什麼樣的網址? –

+0

類似111. ###。###。###:8082/demo(戰爭文件的名稱) – joejoeso

+0

嘗試將index.html放在webapp –

回答

0

您可以將您的index.html下的web應用,因爲這是默認的contextPath,所有其他資源應該是相關的。

-1

也許問題是你必須包含應用程序上下文路徑。

查看以下鏈接做add-context-path-to-spring-boot-application

+0

例如,如果該鏈接失效,您的答案應該能夠自立。如果您提供基本大綱和鏈接以獲取更多信息,那就足夠了,但僅支持鏈接的答案就會被忽視。 – dcsohl

相關問題