04、SpringBoot实战:编写HelloWorld

编写 HelloWorld

    • 1.创建项目
  • 2.修改 Tomcat 端口
  • 3.创建启动类
  • 4.创建 Controller
  • 5.Spring Boot 在 Controller 中常用注解

1.创建项目

2.修改 Tomcat 端口

  • 在resources中创建Application.yml
  • 如果没有创建yml的快捷方式可以参考博文,传送门!!!
    *
  • 注意:如果在IDEA中书写YML文件时没有命令提示,可以参考博文:传送门!!!

3.创建启动类

package com.dqcgm.springboothelloword;

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

@SpringBootApplication
public class SpringBoothellowordApplication {
   
     
    public static void main(String[] args) {
   
     
        SpringApplication.run(SpringBoothellowordApplication.class,args);
    }
}

4.创建 Controller

package com.dqcgm.springboothelloword.controller;

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

@RestController //@Controller+@ResponsBody 直接返回字符串
public class HelloWorldController {
   
     
    @RequestMapping("/helloword")
    public String showHellowWorld(){
   
     
        return "HelloWord";
    }
}

运行结果:
*

5.Spring Boot 在 Controller 中常用注解

  • @RestController
    @RestController 相当于@Controller+@ResponseBody 注解
    如果使用@RestController 注解 Controller 中的方法无法返回页面,相当于在方法上面自动加了 @ResponseBody 注解 ,所以没办法跳转并传输数据到另一个页面,所以 InternalResourceViewResolver 也不起作用,返回的内容就是 Return 里的内容。
  • @GetMapping
    @GetMapping 注解是@RequestMapping(method = RequestMethod.GET)的缩写
  • @PostMapping
    @PostMapping 注解是@RequestMapping(method = RequestMethod.POST)的缩写。
  • @PutMapping
    @PutMapping 注解是@RequestMapping(method = RequestMethod.PUT)的缩写
  • @DeleteMapping
    @DeleteMapping 注解是@RequestMapping(method = RequestMethod.DELETE)的缩写。

版权声明:本文不是「本站」原创文章,版权归原作者所有 | 原文地址: