0

H all,

I am trying to make a simple mvc spring boot application and I have it in my code to return index.html when the controller receives as request for "/".

I am not sure but this does not work.

SpringDataWebApplication.java

    package com.demo.main;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

@SpringBootApplication
@EnableAutoConfiguration
@EnableJpaRepositories("com.demo.repositories.*")
@EntityScan("com.demo.entities.*")
@ComponentScan(basePackages = {"com.demo.controller.*","com.demo.service.*"})
public class SpringDataWebApplication {

    //TODO : Logging


    public static void main(String[] args) {

        SpringApplication.run(SpringDataWebApplication.class, args);


    }

}

HealthCHeckController.java

package com.demo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class HealthCheckController {

    @RequestMapping("/")
    public String getSystemHealth() {
        return "index";
    }

}

UserRepository.java

package com.demo.repositories;

import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

import com.demo.entities.UserEntity;

@Repository
public interface UserRepository extends CrudRepository<UserEntity, String> {

}

index.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="ISO-8859-1">
<title>BlogPost URL check</title>
</head>
<body>
<h1>HealthCHeck Working as desired</h1>
</body>
</html>

Index.html is inside /templates directory as per thymeleaf

enter image description here

pom.xml

<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.demo</groupId>
    <artifactId>Example1</artifactId>
    <version>0.0.1-SNAPSHOT</version>

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

    <properties>
        <java.version>1.8</java.version>
    </properties>

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

        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

        <dependency>
            <groupId>org.modelmapper</groupId>
            <artifactId>modelmapper</artifactId>
            <version>0.7.4</version>
        </dependency>


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

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

    </dependencies>

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



</project>

UserEntity.java : this is not yet required, since I am going step by step and i am expecting just a response for the request "/" but I am not able to get the response.

package com.demo.entities;

import java.io.Serializable;
import java.util.Calendar;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;

@Entity
@Table(name="user")
public class UserEntity implements Serializable{

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    private String Id;
    private String firstName;
    private String lastName;
    private String address1;
    private String address2;
    private String city;
    private String state;
    private String zipcode;
    private Calendar birthDate;
    private String gender;
    private String email;
    private String verifyEmail;
    private String password;
    private String verifyPassword;
    private String createdBy;
    private String modifiedBy;
    private Calendar createdAt;
    private Calendar modifiedAt;

    public UserEntity() {

    }

    @GeneratedValue(strategy=GenerationType.SEQUENCE)
    @NotNull
    public String getId() {
        return Id;
    }

    public void setId(String id) {
        Id = id;
    }


    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getAddress1() {
        return address1;
    }

    public void setAddress1(String address1) {
        this.address1 = address1;
    }

    public String getAddress2() {
        return address2;
    }

    public void setAddress2(String address2) {
        this.address2 = address2;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public String getZipcode() {
        return zipcode;
    }

    public void setZipcode(String zipcode) {
        this.zipcode = zipcode;
    }

    public Calendar getBirthDate() {
        return birthDate;
    }

    public void setBirthDate(Calendar birthDate) {
        this.birthDate = birthDate;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    @javax.persistence.Id
    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getVerifyEmail() {
        return verifyEmail;
    }

    public void setVerifyEmail(String verifyEmail) {
        this.verifyEmail = verifyEmail;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getVerifyPassword() {
        return verifyPassword;
    }

    public void setVerifyPassword(String verifyPassword) {
        this.verifyPassword = verifyPassword;
    }

    public String getCreatedBy() {
        return createdBy;
    }

    public void setCreatedBy(String createdBy) {
        this.createdBy = createdBy;
    }

    public String getModifiedBy() {
        return modifiedBy;
    }

    public void setModifiedBy(String modifiedBy) {
        this.modifiedBy = modifiedBy;
    }

    public Calendar getCreatedAt() {
        return createdAt;
    }

    public void setCreatedAt(Calendar createdAt) {
        this.createdAt = createdAt;
    }

    public Calendar getModifiedAt() {
        return modifiedAt;
    }

    public void setModifiedAt(Calendar modifiedAt) {
        this.modifiedAt = modifiedAt;
    }



}
user641887
  • 1,506
  • 3
  • 32
  • 50
  • what exception are you getting ? – Rajith Pemabandu Apr 10 '17 at 00:59
  • Also I suppose your `UserEntity` class `id` should be `Long` in order to support CrudRepository. [link](http://docs.spring.io/spring-data/commons/docs/1.5.0.RELEASE/reference/html/repositories.html) – Rajith Pemabandu Apr 10 '17 at 01:35
  • the thing is that I dont get any exception. its just the request is not reaching the controller and I get a Whitelabel Error Page when i do http://localhost:8080 – user641887 Apr 10 '17 at 05:53
  • at the first look I do not see an error. Can you show your `Whitelabel Error Page` exception test and the stacktrace – Patrick Apr 10 '17 at 06:40

1 Answers1

1

You should definitely read this:

http://docs.spring.io/spring-boot/docs/1.5.2.RELEASE/reference/htmlsingle/#boot-features-spring-mvc-static-content

You are trying to hit a simple HTML page which is actually a static content. From Spring Boot documentation:

By default Spring Boot will serve static content from a directory called /static (or /public or /resources or /META-INF/resources) in the classpath or from the root of the ServletContext.

First, which means you need to make sure that index.html page is under the static folder, the static folder in Spring boot located under src/main/resources.

Second, When you try to serve static content in spring boot, you'll need and want to bust the cache, that means that your browser is caching your older requests, and in order to see that your changes are taking place you'll need to add this to your application.properties file:

spring.resources.chain.strategy.content.enabled=true
spring.resources.chain.strategy.content.paths=/**

take a look also here:

Spring boot cannot load css from user folder

Community
  • 1
  • 1
Moshe Arad
  • 3,587
  • 4
  • 18
  • 33
  • I do not see any issue of his package structure. His `index.html` is under `src/main/resources/templates`. Thats the normal behavior for thymeleaf and spring. – Patrick Apr 10 '17 at 12:18
  • The main issue is not with the location of the index file as you pointed out, in order to bust the browser cache you suppose to add the above two statements to your application.properties file. and of course, in spring boot it would help if the index file will be under the appropriate location. please read the link to spring boot documentation. – Moshe Arad Apr 10 '17 at 12:33
  • but he is using thymeleaf as template engine. And therefore the location of index.html is under templates. – Patrick Apr 10 '17 at 12:45
  • You are right about the thymeleaf file location, however read this in order to figure out how to serve static content: http://docs.spring.io/spring-boot/docs/1.5.2.RELEASE/reference/htmlsingle/#boot-features-spring-mvc-static-content – Moshe Arad Apr 10 '17 at 12:48
  • he needs to add this to his application.properties file: spring.resources.chain.strategy.content.enabled=true spring.resources.chain.strategy.content.paths=/** – Moshe Arad Apr 10 '17 at 12:50
  • sorrry, but he does not even include any static stuff in the head part of index.html. And this has nothing to do with a whitelabeled error page. And in you provided doc you can read where the stuff gets picked up and its dynamic html. [mvc-template-engines](http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-developing-web-applications.html#boot-features-spring-mvc-template-engines) – Patrick Apr 10 '17 at 13:00
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/141361/discussion-between-moshe-arad-and-patrick). – Moshe Arad Apr 10 '17 at 13:02