寻找写代码感觉(三)之使用 Spring Boot 编写接口

一、前言

项目配置完之后,接着就是写接口了,那咱们就开始吧。

二、项目配置补充知识点

上篇文章写的是关于项目属性配置的一些知识,这里针对上次遗忘内容进行补充如下:

2.1、获取配置文件的值

  1. application.yml 文件中,示例内容如下:
server:
  port: 8888
name: xiaoqiang
age: 11

注意:这里关于yml文件的书写,使用@Value取值时,配置文件的字段一定要顶格写,如:name: xiaoqiang因为空格会认为是某个对象的属性,这里一定要注意

  1. 利用@Value 注解取值

示例接口如下:


@RestController
public class HelloController {

    @Value("${name}")
    private String name;

    @GetMapping("/say")
    public String say(){
        return "hi ,"+name;
    }
}

  1. 访问say接口,就可以获取到值
GET http://localhost:8888/say

HTTP/1.1 200 
Content-Type: application/json
Content-Length: 15
Date: Thu, 26 Aug 2021 07:33:02 GMT
Keep-Alive: timeout=60
Connection: keep-alive

hi ,xiaoqiang

2.2、使用自定义配置类

如果属性很多,我们每个属性都需要写,显得有些费事,我们可以利用自定义配置类进行获取

  1. 修改yml 文件
server:
  port: 8888
name: xiaoqiang
#学生对象的属性
student:
  name: alex
  age: 18

  1. 创建Student.java

示例代码如下:


import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * @author longrong.lang
 * @version 1.0
 * @description
 * @date 2021/8/17 21:16
 */
@Data
@Component
@ConfigurationProperties(prefix = "student")
public class Student {
    private String name;
    private int age;
}

  1. pom文件中引入
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
  1. 接口示例
@RestController
public class HelloController {

    @Resource
    private Student student;

    @GetMapping("/sayToStudent")
    public String sayToStudent(){
        return "hi,"+student.getName();
    }

}
  1. 验证结果
GET http://localhost:8888/sayToStudent

HTTP/1.1 200 
Content-Type: application/json
Content-Length: 7
Date: Thu, 26 Aug 2021 07:49:24 GMT
Keep-Alive: timeout=60
Connection: keep-alive

hi,alex

Response code: 200; Time: 165ms; Content length: 7 bytes

三、神秘的controller

可能很多同学会好奇,接口都在哪写? controller里写呀。

3.1、关于controller

用于定义接口,是请求的入口

3.2、常用注解的使用

  • @RestController注解用于声明返回文本数据,一般返回JSON
  • @Controller注解用于声明返回界面
  • @RestController = @Controller + ResponseBody

3.3、接口的常用写法及参数使用

使用不同路径访问同一个方法

示例代码如下:


        /**
         * 使用不同路径访问同一个方法
        * @return
        */
        @RequestMapping(method= RequestMethod.GET,value = {"/hello","/hi"})
        //@GetMapping({"/hello","/hi"})
        public String sayHello(){
                return "hello,Spring Boot!";
        }


说明:

@GetMapping("/hello")
等价于
@RequestMapping(value="/hello", method=RequestMethod.GET)

@PathVariable:的使用

示例代码如下:

@RestController
public class HelloController {
    /**
     * @PathVariable 使用示例
     * @param age
     * @return
     */
    @GetMapping("/getStudentAge/{age}")
    public int getAge(@PathVariable("age") Integer age){
        return age;
    }
}

访问http://localhost:8888/getStudentAge/111, 结果如下

GET http://localhost:8888/getStudentAge/111

HTTP/1.1 200 
Content-Type: application/json
Transfer-Encoding: chunked
Date: Thu, 26 Aug 2021 08:07:18 GMT
Keep-Alive: timeout=60
Connection: keep-alive

111

Response code: 200; Time: 193ms; Content length: 3 bytes

@RequestParam的使用

正常请求,示例代码如下:

@RestController
public class HelloController {
    /**
     * @RequestParam使用示例
     * @param name
     * @return
     */
    @GetMapping("/getStudentName")
    public String getStudentName(@RequestParam String name){
        return "name :"+name;
    }

}

访问http://localhost:8888/getStudentName?name=111, 结果如下:

GET http://localhost:8888/getStudentName?name=111

HTTP/1.1 200 
Content-Type: application/json
Content-Length: 9
Date: Thu, 26 Aug 2021 08:14:13 GMT
Keep-Alive: timeout=60
Connection: keep-alive

name :111

Response code: 200; Time: 172ms; Content length: 9 bytes

设置参数非必须的,并且设置上默认值

@RestController
public class HelloController {

    /**
     * @RequestParam 设置参数非必须的,并且设置上默认值
     * @param name
     * @return
     */
    @GetMapping("/getStudentInfo")
    public String getStudentInfo(@RequestParam(value = "name",required = true,defaultValue = "rongrong") String name,@RequestParam(value = "age",required = true,defaultValue = "19")int age){
        return "name :"+name+" \t age: "+age;
    }

}

访问http://localhost:8888/getStudentInfo?name=111&age=11, 结果如下:

GET http://localhost:8888/getStudentInfo?name=111&age=11

HTTP/1.1 200 
Content-Type: application/json
Content-Length: 19
Date: Thu, 26 Aug 2021 08:29:12 GMT
Keep-Alive: timeout=60
Connection: keep-alive

name :111     age: 11

Response code: 200; Time: 48ms; Content length: 19 bytes

到此,Controller中接口的写法介绍完成。

寻找写代码感觉(三)之使用 Spring Boot 编写接口

上一篇:C语言 野指针 - C语言零基础入门教程


下一篇:Java注释(书写注释是一个良好的习惯)