SpringBoot---Helloworld
官网:Spring.io
SpringBoot---HelloWorld
在SpringBoot中免去了之前SSM的大量繁琐的配置,在使用时只要引入一个父工程SpringBoot,再引入SpringBoot的场景启动器,就是对应的依赖。
<!--引入SpringBoot的父工程--> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.6.1</version> </parent> <!--引入SpringBoot的依赖--> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>
然后创建一个 类名为:MyApplication 的主类,【假如你的包叫xxx.com,那么MyApplication就在com下面建,后面的bean、dao、controller、mapper在com下建立】使用SpringBoot注解@SpringBootApplication 标记这个类告诉SpringBoot这是一个SpringBoot应用。
在类里面创建main方法,使用 SpringApplication.run(MyApplication.class, args)把主类放进去,后面的args是参数。
/** * 主程序类 * 使用了@SpringBootApplication表示的类说明这是一个SpringBoot应用 */ @SpringBootApplication public class MyApplication { public static void main(String[] args) { ConfigurableApplicationContext run = SpringApplication.run(MyApplication.class, args); } }
现在想要实现发送请求浏览器响应返回HelloWorld
只需要在MyApplication类同一级目录下创建controller,使用@RestController声明类和方法,在controller的方法上面使用注解@RequestMapping设置好请求路径,之后在方法内部设置返回值。
//声明类是controller的同时也声明类里面所有方法的返回值能直接相应在页面上。@Controller+@ResponseBody @RestController public class TestController { @RequestMapping("/") public String test01(){ return "Hello,SpringBoot2.6.1"; } }
设置好后,来到主程序类中,启动main方法,就可以去浏览器上访问这个地址了。直接http://localhost:8080/ 就可以看见返回了一个Hello,SpringBoot2.6.1
在SpringBoot里面就集成了tomcat,以及很多配置项、引入很多依赖项,这些工作SpringBoot都做好了。

浙公网安备 33010602011771号