-
首先需要在springboot的启动类上面使用@SpringBootApplication注解,并且指定扫描的包的位置,如下:
package com.example;
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication(scanBasePackages="com.example.controller") public class DemoApplication {
public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); }
} 这里如过需要扫描多个包可以这么写scanBasePackages={"com.xxx","com.xxx"}这种形式即可
2.其次在当前的pom.xml中指定springboot启动类:
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <!-- 这里是我本人的springboot启动类位置,请根据自己的情况改动,idea下面可以点出来的--> <start-class>com.example.DemoApplication</start-class> </properties> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <fork>true</fork> <mainClass>${start-class}</mainClass> </configuration> </plugin> </plugins> </build>
3.这里是我的controller:
package com.example.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping("/home")
public class TestController {
@RequestMapping("/hello")
@ResponseBody
public String index(){
return "hello world";
}
}
注意:以上的springboot版本是2.0.5.RELEASE版,不同版本可能会有所不同。 springboot启动后浏览器输入下面的URL即可 http://localhost:8080/home/hello