简体   繁体   中英

Running spring boot on server

I have finally finished writing a very "simplistic" hello world project.
When I run it from my IDE, it works just fine.
When I run the jar on Windows 10 and I go to localhost (I am running on port 80) then I see the webpage and everything works just fine.
Once I drop that same jar on my debian server that has same mysql and java installed (and nothing else installed or configured) as my windows machine, and I try to navigate to the ip address I get

Whitelabel Error Page

This application has no explicit mapping for /error, so you are seeing this as a fallback.

Sun Feb 26 02:54:40 UTC 2017
There was an unexpected error (type=Not Found, status=404).
Not Found

This tells me that the spring boot is running (because the second I shut it off I can't access the same page anymore), however I can not seem to navigate to any of the pages that I could on my windows machine.

I have absolutely no idea what I am doing wrong here.

EDIT:

Thanks to Oleksandr Shpota I am seeing why things do not work. My jar doesn't contain my freemarker files.

My build.gradle

apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'jetty'
apply plugin: 'war'
apply plugin: 'org.springframework.boot'

repositories {
    mavenCentral()
}

dependencies {
    compile("org.springframework.boot:spring-boot-starter-web:1.5.1.RELEASE") {
        exclude module: "spring-boot-starter-tomcat"
    }
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-jetty', version: '1.5.1.RELEASE'
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-actuator', version: '1.5.1.RELEASE'
    compile group: 'org.springframework', name: 'spring-webmvc', version: '4.3.6.RELEASE'
    compile group: 'org.springframework', name: 'spring-context-support', version: '4.3.6.RELEASE'
    compile group: 'org.springframework', name: 'spring-orm', version: '4.3.6.RELEASE'
    compile group: 'javax.servlet', name: 'javax.servlet-api', version: '3.1.0'
    compile group: 'org.freemarker', name: 'freemarker', version: '2.3.23'
    compile group: 'mysql', name: 'mysql-connector-java', version: '6.0.5'
    compile group: 'org.hibernate', name: 'hibernate-core', version: '5.1.4.Final'
    compile group: 'commons-validator', name: 'commons-validator', version: '1.5.1'
    compile group: 'junit', name: 'junit', version: '4.12'
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-test', version: '1.5.1.RELEASE'
}

task wrapper(type: Wrapper) {
    gradleVersion = '2.2'
}

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.5.1.RELEASE")
    }
}

jar {
    from {
        (configurations.runtime).collect {
            it.isDirectory() ? it : zipTree(it)
        }
    }
    baseName = 'carefree-cooking'
    version = '0.1.0'
    manifest {
        attributes 'Main-Class': 'carefree.cooking.Application'
    }
}

springBoot {
    executable = true
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

My Beans for configuring FreeMarker

@Bean(name = "freemarkerConfig")
public FreeMarkerConfigurer freemarkerConfig()
{
    FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
    configurer.setTemplateLoaderPath("/");
    return configurer;
}

@Bean(name = "viewResolver")
public FreeMarkerViewResolver viewResolver()
{
    FreeMarkerViewResolver viewResolver = new FreeMarkerViewResolver();
    viewResolver.setPrefix("WEB-INF/template/");
    viewResolver.setSuffix(".ftl");
    return viewResolver;
}

My index controller

@RequestMapping()
public String index(ModelMap model)
{
    log.info("======= We are in index controller going to load page");
    return "page";
}

And the page.ftl is in src\\main\\webapp\\WEB-INF\\template\\page.ftl

So the problem here is that when I build my jar, page.ftl is not included and neither is any of the webapp directory.

Your templates are in the wrong directory. You are creating a jar file which isn't a war file. However you have put the files in the location as it was a war file.

Also Spring Boot has Freemarker support out of the box so I would suggest you to use that, instead of manually configuring it yourself. So to fix.

  1. Remove your Freemarker configuration
  2. Move templates to src/main/resources/templates

Then restart your application.

As a bonus I suggest to cleanup your build.gradle . Remove the explicit versions for the starters and remove some additional dependencies because they are already included through the starters. You don't need the jetty and war plugins that is covered by Spring Boot.

And your jar plugin is breaking the default Spring Boot functionality, Spring Boot plugin takes care of that already.

apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'

repositories {
    mavenCentral()
}

dependencies {
    compile("org.springframework.boot:spring-boot-starter-web") {
        exclude module: "spring-boot-starter-tomcat"
    }
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-jetty''
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-actuator'
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-freemarker'

    compile group: 'org.springframework', name: 'spring-orm'

    compile group: 'mysql', name: 'mysql-connector-java'
    compile group: 'org.hibernate', name: 'hibernate-core'
    compile group: 'commons-validator', name: 'commons-validator', version: '1.5.1'
    compileTest group: 'org.springframework.boot', name: 'spring-boot-starter-test'
}

task wrapper(type: Wrapper) {
    gradleVersion = '3.3'
}

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.5.1.RELEASE")
    }
}

springBoot {
    executable = true
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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