0
点赞
收藏
分享

微信扫一扫

SpringBoot2快速入门01--工程创建

西曲风 2021-09-24 阅读 30

首先,通过https://start.spring.io/创建一个Gradle工程,Dependencies选择web,然后Generate Project。

用idea导入工程,启动发现报错,是因为没有导入web包,需要手动添加,如下:
compile('org.springframework.boot:spring-boot-starter-web')
然后就可以启动了。

下面配置application.yml,如下:

server:
  port: 8081
  servlet:
    context-path: /chapter01   #系统url前缀

上面的是配置端口和上下文路径。

springboot给我们预加载了一些对象,若要知道,可以通过CommandLineRunner输出,如下:

@Bean
    public CommandLineRunner commandLineRunner(ApplicationContext ctx) {
        // 目的是
        return args -> {
            System.out.println("SpringBoot 默认为我们提供的 Bean:");
            String[] beanNames = ctx.getBeanDefinitionNames();
            Arrays.sort(beanNames);
            Arrays.stream(beanNames).forEach(System.out::println);
        };
    }

接着写一个简单的controller,输出一个字符,如下:

@RestController
public class Chapter01Controller {
    @GetMapping("/demo1")
    public String demo1() {
        return "Hello iti";
    }
}

启动服务,在浏览器输入:http://localhost:8081/chapter01/demo1

举报

相关推荐

0 条评论