springboot之RESTful接口与Swagger

本文介绍了RESTful架构中的基本操作方法如GET、POST、PUT、DELETE,并强调了安全性与幂等性的概念。同时,展示了如何在SpringBoot应用中使用Swagger生成、配置和可视化RESTfulAPI,包括添加Swagger资源处理器和定制ApiInfo。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、RESTful

GET获取资源、POST新建资源、PUT更新资源、DELETE删除资源。

RESTful两大特性

1、安全性:GET请求不会引起资源本身改变。

2、幂等性:对一个接口请求和多次请求返回的资源应该一致。

2xx:成功

4xx:客户端错误。

5xx:服务器错误。

URI中不要包含动词

通过使用PathVariable注解获取{id}

@GetMapping("/user/{id}")
    public String getUserById(@PathVariable int id){
        System.out.println(id);
        return "根据ID获取用户信息";
    }

二、Swagger

用于生成、描述、调用和可视化RESTful风格的Web服务。

配置方法

添加包。这里使用到的swagger2的版本是2.9.2记得把springboot的版本改成2.5.6!!!

<!-- swagger2相关功能       -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>
        <!--  swagger2ui      -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>

创建SwaggerConfig.java文件,专门放在config包下。
在这里插入图片描述

把代码放进去就好了。
注意这里重写了addResourceHandlers方法

package com.example.hello.config;


import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;


//Spingboot中配置类必须要加注解
@Configuration
@EnableSwagger2
public class SwaggerConfig implements WebMvcConfigurer {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

    /*
    * 配置swagger2相关Bean
    * */

    @Bean
    public Docket createRestApi(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com"))   //com包下所有API都交给Swagger2管理
                .paths(PathSelectors.any())
                .build();
    }

    /*
    * 此处主要是API文档页面显示信息
    * */
    private ApiInfo apiInfo(){
        return new ApiInfoBuilder()
                .title("演示项目API")   //标题
                .description("演示项目")    //描述
                .version("1.0")
                .build();
    }


}

http://localhost:8080/swagger-ui.html/
然后就成功了

在这里插入图片描述

除此之外,还能给每个接口增加说明,需要到后端指定接口处去增加。

例如说,我在hello2这个方法增加了注解,要使用@ApiOperation("")注解

在这里插入图片描述

在swagger展示如下。在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

牛右刀薛面

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值