01--SpringBoot之初体验

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_30447263/article/details/81085061
一.创建一个项目:IDE为IDEA

创建流程

二.新建一个Controller类:

toly1994.com.toly01.controller.HelloSpringBoot

@RestController 
public class HelloSpringBoot {
    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public String say() {
        return "HelloSpringBoot";
    }
}

访问效果


注解的方式还有一种,和上面的效果一样

@RestController
public class HelloSpringBoot {
    @GetMapping("/hello")
    public String say() {
        return "HelloSpringBoot!";
    }
}
注解说明:
@RestController:处理http请求:等同于@Controller+@ResponseBody
@RequestMapping: 请求的相关参数、url映射 value = "访问的路由" method = 请求方法
@GetMapping:以GET方式请求 相当于对@RequestMapping配置的缩写
三.url的其他形式:

类和方法都有value时

@RestController
@RequestMapping(value = "/hello")
public class HelloSpringBoot {
     @GetMapping("/say")
    public String say() {
        return "HelloSpringBoot!!";
    }
}

配置多url对1映射:

@RestController
public class HelloSpringBoot {
    @GetMapping({"/hello", "/hi"})
    public String say() {
        return "HelloSpringBoot!!!";
    }
}

四.配置文件使用:

修改配置方式1:src\main\resources\application.properties

#修改端口号
server.port=8081
#端口号后需额外添加字符
server.servlet.context-path=/toly01
toly1994.com.toly01.controller.HelloSpringBoot
@RestController
public class HelloSpringBoot {
    @GetMapping("/hello")
    public String say() {
        return "HelloSpringBoot!";
    }
}
修改配置方式2:src\main\resources\application.yml
server:
  port: 8081
  servlet:
    context-path: /springboot

个人比较喜欢yml


五.切换配置文件
src\main\resources\application-dev.yml
server:
  port: 8081
src\main\resources\application-prod.yml
server:
  port: 8080
src\main\resources\application.yml:根据active:来切换配置文件
spring:
  profiles:
    active: prod

猜你喜欢

转载自blog.csdn.net/qq_30447263/article/details/81085061