spring boot入门小案例

时间:2022-09-14 11:17:02

spring boot 入门小案例搭建

(1) 在Eclipse中新建一个maven project项目,目录结构如下所示:

spring boot入门小案例

cn.com.rxyb中存放spring boot的启动类,application.properties中放spring boot相关配置

(2) 在pom.xml中加入spring boot 依赖包

spring boot入门小案例

(3)在cn.com.rxyb中新建启动类APP

 package cn.com.rxyb;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; @SpringBootApplication
@EnableAutoConfiguration(exclude= {DataSourceAutoConfiguration.class})
public class App { public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}

APP.java启动类

继续在包中创建测试Controller控制器

 package cn.com.rxyb;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
public class TestController { @GetMapping("hello")
public String helloword() {
return "hello";
} @GetMapping("hello2")
public String helloworkd2() {
return "hello2";
}
}

TestController.java控制器

(4)接下来可以在启动类APP中右键--->run as---->java Application来启动spring boot。之后打开浏览器输入http://localhost:8080/hello直接访问。

spring boot入门小案例

如果想在hello前面加名字空间的话,可以在application.properties中做如下配置:

server.context-path=/demo

  这样我们就在hello之前加入了/demo名字空间,在访问hello的时候,hello之前必须加/demo名字空间才可以访问

spring boot入门小案例

今天的spring boot入门小案例学习就到这里,明天继续。