深圳专业建网站技术支持 昆明网站建设
web/
2025/9/27 12:57:09/
文章来源:
深圳专业建网站,技术支持 昆明网站建设,沈阳建站经验,做一个网址需要多少钱一、RESTful
GET获取资源、POST新建资源、PUT更新资源、DELETE删除资源。
RESTful两大特性
1、安全性#xff1a;GET请求不会引起资源本身改变。
2、幂等性#xff1a;对一个接口请求和多次请求返回的资源应该一致。
2xx#xff1a;成功
4xx#xff1a;客户端错误。 …一、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相关功能 --dependencygroupIdio.springfox/groupIdartifactIdspringfox-swagger2/artifactIdversion2.9.2/version/dependency!-- swagger2ui --dependencygroupIdio.springfox/groupIdartifactIdspringfox-swagger-ui/artifactIdversion2.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 {Overridepublic 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* */Beanpublic 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展示如下。
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/web/81118.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!