001 - 使用Spring Boot新建一个RESTful的Web服务

目标

  • 每5秒打印一次时间

实现

1. Maven配置

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

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>
  • spring-boot-maven-plugin这个插件的作用是将项目打包为可直接运行的jar包,使用java -jar可直接运行

2. 编写ScheduledTasks

@Component
public class ScheduledTasks {
    private static final Logger log = LoggerFactory.getLogger(ScheduledTasks.class);

    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");

    @Scheduled(fixedRate = 5000)
    public void reportCurrentTime() {
        log.info("The time is now {}", dateFormat.format(new Date()));
    }
}

@Scheduled

  • 以有多种配置形式,支持cron表达式

3. 编写Spring Boot 启动类

@SpringBootApplication
public class RestServiceApplication {

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

@SpringBootApplication

包括:@Configuration、@EnableAutoConfiguration和@ComponentScan

@Configuration:将该类标记为资源bean

@EnableAutoConfiguration:告诉spring,开始按classpath 中的设置配置bean,如果没有就自动配置

@ComponentScan:让系统去对应的包下寻找bean

REST

来源:链接

  • 以往都是以功能为核心设计API
  • 不同的操作之间有关联,容易产生冗余代码或者逻辑错误

  • URL设计容易混乱

  • RESTful以资源为核心进行设计API

1、 非RESTful风格设计增删改查接口

@RequestMapping("/queryStudent")
@RequestMapping("/insertStudent")
@RequestMapping("/deleteStudent")
@RequestMapping("/updateStudent")

2、 RESTful风格设计接口

一个接口实现了增删改查

@GetMapping("/student")
@PutMapping("/student")
@DeleteMapping("/student")
@PostMapping("/student")
  1. @GetMapping = @RequestMapping(method = RequestMethod.GET)
    作用:对应查询,表明是一个查询URL映射

  2. @PostMapping = @RequestMapping(method = RequestMethod.POST)的简写
    作用:对应增加,表明是一个增加URL映射

  3. @PutMapping = @RequestMapping(method = RequestMethod.PUT)的简写
    作用:对应更新,表明是一个更新URL映射

  4. @DeleteMapping = @RequestMapping(method = RequestMethod.DELETE)的简写
    作用:对应删除,表明是一个删除URL映射

  5. @PatchMapping
    作用:对put方式的一种补充,put是整体更新,patch是局部更新

3、 RESTful 入参的注解

@PathVariable

获取路径参数,即url/{id}这种形式,用于将请求URL中的模板变量映射到功能处理方法的参数上。

@RequestMapping(“item/{itemId}”)

@RequestParam

获取查询参数,即url?name=这种形式,主要参数:

  • value:参数名字,即入参的请求参数名字,如username表示请求的参数中的名字为username的参数的值将传入;
  • required:是否必须,默认是true,表示请求中一定要有相应的参数,否则将报404错误码;
  • defaultValue:默认值,表示如果请求中没有同名参数时的默认值,例如:
public List getItemTreeNode(@RequestParam(value=“id”,defaultValue=“0”)long parentId)

@RequestBody

  • 常用来处理content-type不是默认的application/x-www-form-urlcoded编码的内容,比如说:application/jsonapplication/xml
  • 可以将请求体中的JSON字符串绑定到相应的bean上,当然,也可以将其分别绑定到对应的字符串上

@ModelAttribute

  • 在使用RESTful风格时,使用get请求,又想使用对象接收参数,就可以使用这个注解
  • 不光适用于get请求,同样也适用于put和delete请求

运行

  • 直接用idea中的按钮运行或者用下列命令:
mvn clean package
java -jar target/gs-rest-service-0.1.0.jar

结语

  • 工作六个月了,从一无所知只会看视频学习的小白,现在也有勇气去看英文教程了
  • 虽然不在大公司,但是还是希望能精进自己的技术,希望能坚持下去
posted @ 2021-07-26 15:09  回忆yi生淡忘  阅读(70)  评论(0)    收藏  举报