Springboot完成Hello World

研究下springboot,万物源自Hello World

直接通过IDEA基于Maven工程完成

首先创建一个project,基于Default的spring.io

然后自行配置,在Dependencies里可以选中WEB,也可以不用管,pom里面自己来配置

创建之后,会从spring.io下载一个原始demo,大致文件目录

NewImage

先在pom里面新增依赖

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

此时com.lihuia.demo下面再创建一个controller包,新增一个控制器的类

@RestController是spring的注解,提供REST API,可以服务JSON,XML或者其他,渲染出一个字符串

@RequestMapping提供路由信息,”/helloworld”这个路径的HTTP Request会映射到sayHello()方法进行处理

package com.lihuia.demo.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
* Copyright (C), 2018-2019
* FileName: HelloController
* Author: lihui
* Date: 2019/5/22
*/

@RestController
public class HelloController {

@RequestMapping("/helloworld")
public String sayHello() {
return "Hello World!";
}
}

在application.properties里修改一下启动端口,默认是8080

server.port=8888

最后启动类DemoApplication的main方法

@SpringBootApplication注解为Spring Boot应用的标识

main函数入口,SpringApplication引导引用,将DemoApplication作为参数传递给run方法,然后会启动嵌入式Tomcat并初始化Spring环境以及各个Spring组件

package com.lihuia.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

public static void main(String[] args) throws IllegalAccessException {

SpringApplication.run(DemoApplication.class, args);
}

}

启动日志如下:

 
  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.5.RELEASE)

2019-05-22 02:07:36.098  INFO 14096 --- [           main] com.lihuia.demo.DemoApplication          : Starting DemoApplication on 2018 with PID 14096 (/Users/lihui/caocao/git/demo/target/classes started by lihui in /Users/lihui/caocao/git/demo)
2019-05-22 02:07:36.100  INFO 14096 --- [           main] com.lihuia.demo.DemoApplication          : No active profile set, falling back to default profiles: default
2019-05-22 02:07:36.992  INFO 14096 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8888 (http)
2019-05-22 02:07:37.020  INFO 14096 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2019-05-22 02:07:37.020  INFO 14096 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.19]
2019-05-22 02:07:37.122  INFO 14096 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2019-05-22 02:07:37.122  INFO 14096 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 982 ms
2019-05-22 02:07:37.320  INFO 14096 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2019-05-22 02:07:37.512  INFO 14096 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8888 (http) with context path ''
2019-05-22 02:07:37.515  INFO 14096 --- [           main] com.lihuia.demo.DemoApplication          : Started DemoApplication in 1.76 seconds (JVM running for 2.25)

由于端口改成了8888,直接本地浏览器输入http://127.0.0.1:8888/helloworld即可返回Hello World!

发表回复