Spring Boot I do not run a Controller when I put it in a different package

0

Good afternoon developers, I'm starting in the world of Spring Boot, I created a simple Spring called AppSqlServerApplication with the Spring Initializr ( link ), The question is that I want to order the application for packages com.example.AppSqlServer, controllers, and entities. Inside the package com.example.AppSqlServer is the main class that starts the application, the problem is that when I create a UserController class inside the controllers package, I do not run, but I pass this class ("UserController") to the com package. example.AppSqlServer where the main class is if executed. Can someone tell me how to solve this? Thank you... This is the file structure:

package controllers;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {

 public UserController() {

 }

 @GetMapping(value = "/homeUser")
 public String HomeUser() {
    return "Hello User";
 }

}
    
asked by Cristian D. 13.03.2018 в 23:34
source

1 answer

1

You have to create your package structure from the package where you have the main class of Spring ( AppSqlServerApplication ). That is, com.example.AppSqlServer.controllers and com.example.AppSqlServer.entities .

Another option is, in AppSqlServerApplication, write down the packages you want to include in the following way:

@ComponentScan({"controllers","entities"})
@SpringBootApplication
public class AppSqlServerApplication {
...

Personally, I prefer the first one, less code and easier to maintain.

    
answered by 14.03.2018 / 15:00
source