@Autowired
private ErrorAttributes errorAttributes;
@Bean
public AppErrorController appErrorController(){return new AppErrorController(errorAttributes);}
当你运行一个 Spring 引导应用程序(例如一个带有@SpringBootApplication 注释的类)时,Spring 只会扫描你的主类包下面的类。
com
+- APP
+- Application.java <--- your main class should be here, above your controller classes
|
+- model
| +- user.java
+- controller
+- UserController.java
package co.companyname.spring.tutorial; // package for main class
@SpringBootApplication
public class FirstProjectApplication {
public static void main(String[] args) {
SpringApplication.run(FirstProjectApplication.class, args);
}
}
package co.companyname.spring.tutorial.controllers; // package for controllers
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@RequestMapping("/hello")
public String hello() {
return "Hello, world";
}}
完成编码后按启动仪表板
最后一件事,以确保您的控制器映射或不只是控制台您应该看到一些微笑
Mapped "{[/hello]}" onto public java.lang.String co.companyname.spring.tutorial.controllers.HelloController.hello()
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan({ "com.controller.package1, com.controller.package2, com.controller.package3, com.controller.packageN", "controller", "service" } // If our Controller class or Service class is not in the same packages we have //to add packages's name like this...directory(package) with main class
public class MainApp {
public static void main(String[] args) {
SpringApplication.run(MainApp.class, args);
}
}
@SpringBootApplication
@RestController
public class Viper {
@RequestMapping("/")
public String home(){
return "This is what i was looking for";
}
public static void main( String[] args){
SpringApplication.run(Viper.class , args);
}
}
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
@RestController
public class HelloController {
@RequestMapping("/")
public String index() {
return "Greetings from Spring Boot!";
}
}
当我学习春天仇恨的时候,我遇到了这个问题。我检查了上面给出的所有答案,但问题没有得到解决。最后,我将我的控制器类粘贴到“ main application.java”包中,它对我很有用。[![你可以在图片中看到,我在一个包中添加了我的控制器类和主类。你也可以添加“模型类,主类和控制器类”在同一个包,也为我工作。在下图中,我在同一个包中添加了控制器和主类。
package com.sprindemo.demohelloword;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloWorldController {
@RequestMapping("/") // <<<<<<<<<<<
public String helloWord() {
return "Hello world from Spring Boot";
}
}
这不常见,根不见了
package com.sprindemo.demohelloword;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloWorldController {
@RequestMapping // <<<<<<<<<<<
public String helloWord() {
return "Hello world from Spring Boot";
}
}