demo1. 快速搭建一个restful service

835 阅读2分钟

参考文档:Springboot RESTful Service Guide


1. demo说明

这是一个,教你如何快速搭建一个springboot脚手架的web服务的方法,可以做一个get方法的接口,符合RESTful规范

下面的步骤中,会从如何初始化一个脚手架开始,在后续的demo中,将不再赘述初始化的步骤

2. 如何构建工程并运行

1. 通过spring initializr官网下载一个springboot脚手架

image.png

注意:如果忘记添加web依赖了,可以手动把依赖配置添加到pom.xml文件中,然后更新maven即可。具体可参考我的另一篇文:juejin.cn/post/703070…

2. 用idea打开项目,并build项目

注意:如果build失败,需要把maven设置为你的本地下载的maven

image.png

3. 创建restfulService Package

image.png

4. 按照教程,分别创建Greeting.java和GreetingController.java两个文件

4.1 Greeting.java

本代码中,主要创建了一个标准的Greeting类,提供了私有变量id和content,并设置了constructor()getId()getContent()等,符合一个标准的java bean类的样式,便于其他java class可以获取id和content的值,却又不直接暴露变量为public类型

package com.example.springbootDemos.restfulService;

public class Greeting {
    private final long id;
    private final String content;

    public Greeting(long id, String content) {
        this.id = id;
        this.content = content;
    }

    public long getId() {
        return id;
    }

    public String getContent() {
        return content;
    }
}

4.2 GreetingController.java

这是一个提供了RESTful服务的控制器,属于MVC中的controller,一方面便于接口可以访问使用greeting类,一方面构建了使用get方法的/greeting接口,便于访问。

其中有几个点需要注意:

  • @RestController:代表这一段使用了REST服务
  • @GetMapping:代表使用了get方法,内部传参代表了接口路径为/greeting
  • 如果是使用post方法,则为@PostMapping
  • @RequestParam:代表请求的param的key是name,值如果没有传的话,就是用默认的字符串World
package com.example.springbootDemos.restfulService;

import java.util.concurrent.atomic.AtomicLong;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class GreetingController {

    private static final String template = "Hello, %s!";
    private final AtomicLong counter = new AtomicLong();

    @GetMapping("/greeting")
    public Greeting greeting(@RequestParam(value = "name", defaultValue = "World") String name) {
        return new Greeting(counter.incrementAndGet(), String.format(template, name));
    }
}

注意:如果两个java文件不在同一个文件夹,则调用时需要import

5. 运行restful service

在同样的restfulService文件夹中,创建GreetingApplication.java类,这里面就是使用SpringApplication的方法,启动了该服务

package com.example.springbootDemos.restfulService;

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

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

点击运行后:

image.png

补充:

如果我们想在com.example.springbootDemos中的application类中,统一对各种service进行启动管理

image.png

所以,我们需要编辑该文件内容,使其可以启动上面的RESTController

package com.example.springbootDemos;

// 这里需要引入restful的启动方法GreetingApplication
import com.example.springbootDemos.restfulService.GreetingApplication;

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

@SpringBootApplication
public class SpringbootDemosApplication {

   public static void main(String[] args) {

      SpringApplication.run(GreetingApplication.class, args);
   }

}

6 测试结果

image.png