Reputation:
my problem is that i dont know how to specify the path for my Controller package to search for requestmapping annotations inside it in the @componenscan annotation, which is in the main class.
My DemoApplication class:
package com.personalitymeet;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Configuration
@EnableAutoConfiguration
@ComponentScan()
@Controller
public class DemoApplication {
@ResponseBody
@RequestMapping("/")
String entry(){
return "bla";
}
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
Usercontroller.java:
package com.personalitymeet.web;
import com.personalitymeet.domain.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* Created by mv on 15.09.2016.
*/
@Controller
public class UserController {
@RequestMapping("/user")
public String user(Model model){
User user = new User();
user.setFirstname("Misi");
user.setLastname("Varga");
model.addAttribute("user",user);
return "userview";
}
}
So, my question is, how can i tell springboots that it should search for @Requestmapping annotation in the Usercontroller class?
Upvotes: 0
Views: 965
Reputation: 720
Can you replace this code:
@Configuration
@EnableAutoConfiguration
@ComponentScan()
@Controller
public class DemoApplication {
...
With this code:
@SpringBootApplication
@Controller
public class DemoApplication {
...
Configure your application to expose the management endpoints:
http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#production-ready
and then hit this endpoint to see the endpoint exposed by the application:
http://localhost:[port]/manage/mappings
Also, I recommend to create a separate controller for the root to keep your startup class uncluttered with application logic.
Upvotes: 0
Reputation: 77167
Your problem isn't the @ComponentScan
, which will search in com.personalitymeet
and all subpackages. It's that for some reason you don't have the MVC infrastructure activating.
You should have spring-boot-starter-web
as a dependency in your build.gradle
file, and you should add @EnableWebMvc
to your configuration class.
Upvotes: 0
Reputation: 10497
You can specify component scan to your configuration file to scan folder com.personalitymeet
and it will automatically pick all the classes which has relevant annotations.
Below are basic 3 annotations needed to init application.
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.personalitymeet")
Upvotes: 1