Solucionado (ver solução)
Solucionado
(ver solução)
1
resposta

Spring Boot 2

Olá pessoal, tudo bem?

Ao efetuar testes no Spring Boot, não estou conseguindo carregar arquivos estáticos. Segue abaixo estrutura do projeto:

DemoApplication.java

package com.example.demo;

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

@SpringBootApplication
public class DemoApplication {

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

HelloWorldController.java

package com.example.demo.controllers;

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

@Controller
public class HelloWorldController {

    @RequestMapping
    public String helloWorld() {
        return "hello_world";
    }
}

src/main/resources/templates/hello_world.html

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8" />
<title>Insert title here</title>
<link rel="stylesheet" type="text/css" th:href="@{/css/base.css}" />
</head>
<body>
  <h1>Hello Spring Boot!</h1>
</body>
</html>

src/main/resources/static/css/base.css

@charset "UTF-8";

h1 {
  color: #ff0000;
}

build.gradle

buildscript {
    ext {
        springBootVersion = '2.0.0.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
    mavenCentral()
}


dependencies {
    compile('org.springframework.boot:spring-boot-starter-thymeleaf')
    compile('org.springframework.boot:spring-boot-starter-web')
    runtime('org.springframework.boot:spring-boot-devtools')
    testCompile('org.springframework.boot:spring-boot-starter-test')
}

Alguém sabe me informar o por quê desse erro está ocorrendo?

1 resposta
solução!

Olá, pessoal!

Tinha esquecido de colocar @RequestMapping na classe controlador. Ficou assim

package com.example.demo.controllers;

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

@RequestMapping
@Controller
public class HelloWorldController {

    @RequestMapping
    public String helloWorld() {
        return "hello_world";
    }
}