Good afternoon! I\'m starting spring studies, I\'m following a tutorial the same way, but it returns an error:
Folder structure:
The strange th
As answered by Deepak, main class should be in a root package above other packages. But if you don't want to do this, you can use:
@SpringBootApplication(scanBasePackages = {"com.other.packages","com.other"})
public class SpringAppApplication {
.....
}
verify that you have the correct thymeleaf dependency within your pom.xml:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
Make sure that your main class is in a root package above other classes.
When you run a Spring Boot Application, (i.e. a class annotated with @SpringBootApplication), Spring will only scan the classes below your main class package.
So your declaration goes like this
package br.com.SpringApp.SpringApp;
inside this main class i.e SpringAppApplication
package br.com.SpringApp.SpringApp.controller;
name of your controllers i.e EventoController & indexControllers
package br.com.SpringApp.SpringApp.model;
name of your models i.e Evento
After This clean your project and re-run spring boot application;
Solution:
If you are using @Controller
over the Controller class then it will be treated as a MVC controller class. But if you want a special controller used in RESTFul web services then you to use @Controller
along with @ResponseBody
annotation or you can directly use @RestController
over the Controller
class. It worked for me as I was getting the same error while creating SpringBoot project with RestFul webservices.
package br.com.SpringApp.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class EventoController {
@RequestMapping("/cadastroEvento")
@ResponseBody
public String form() {
return "evento/formEvento";
}
}
or:
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@RestController
public class EventoController {
@RequestMapping("/cadastroEvento")
public String form() {
return "evento/formEvento";
}
}
As a beginner to the SpringBoot, this error can be happened to anyone at least one time, because for a few reasons.
@SpringBootApplication
or extends SpringBootApplication
@RestController
annotation at top of the controller class.@PostMapping("/save")
or @GetMapping("/id")
etc.)@RequestMapping("/customer")
(if the annotation available in your controller class) or spellings error in the URL.