0

I have run a simple program which allows me to map HTTP requests to a function - which is great, so I decided that rather than simply return a random string I could just as easily return a HTML page in string format and like that be able to dynamically serve up static HTML pages.

The problem is I am unable to load the web page resource.

My project has this structure:

projectFolder
    pom.xml
    -> src
         -> main
              -> webapp
                 home.html
              -> java
                   -> redway
                         MLoader.java
                         Application.java

As I say my program works fine if I just want to return a string, it is trying to load the home.html file that I can't seem to manage, specifically my problem is how to get the correct path to it.

This is the last thing I tried:

package redway;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.net.URL;
import java.io.*;

@RestController
public class MLoader {

    public static final String HOME_HTML = "src/main/webapp/home.html";

    @RequestMapping("/home")
    public String home() {

        String page = "";
        String line;

        URL url = this.getClass().getClassLoader().getResource(HOME_HTML);

        try (BufferedReader reader = new BufferedReader(new InputStreamReader (new FileInputStream(url.toString()),"UTF-8"))){
            while((line = reader.readLine()) != null) {
                page += line;
            }
        } catch (IOException e) {
            return "unable to load file...";
        }

        return page;
    }

}

Here is my pom.xml :

<?xml version="1.0" encoding="UTF-8"?>
<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>org.springframework</groupId>
    <artifactId>HTMLLoader</artifactId>
    <version>1</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.2.3.RELEASE</version>
    </parent>

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

    <properties>
        <java.version>1.7</java.version>
    </properties>


    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>spring-releases</id>
            <url>https://repo.spring.io/libs-release</url>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>spring-releases</id>
            <url>https://repo.spring.io/libs-release</url>
        </pluginRepository>
    </pluginRepositories>
</project>

And just for good measure here is my Apllication.java file containing my main method:

package redway;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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

I've been fiddling with it for a couple of hourse and it's driving me mad because I'm sure there must be a really simple explanation. I am just playing with it at the moment, so I'm sure this is not the best approach to building a dynamic web app, but I would still like to know what I have done wrong.

kryger
  • 12,906
  • 8
  • 44
  • 65
Sam Redway
  • 7,605
  • 2
  • 27
  • 41

1 Answers1

0

maven approach is to put html files (or any other resource files) under src/main/resources/

after that try

this.getClass().getResource("/home.html") 
  • It compiles but it throws this error at runtime: Fri May 15 21:35:41 BST 2015 There was an unexpected error (type=Internal Server Error, status=500). No message available – Sam Redway May 15 '15 at 20:36
  • That me me think so I looked in the actual jar file to check its location and I can't find the resource in the jar. This may sound stupid because I'm new to Maven, but do I need to include it in the pom.xml file? I'm thinking maybe it was never compiled into the Jar? – Sam Redway May 15 '15 at 20:41
  • check your jar in target folder like jar tf target/your-name.jar – Vladimir Dolzhenko May 15 '15 at 20:44