美文网首页
springBoot使用

springBoot使用

作者: 测试员 | 来源:发表于2019-11-03 16:40 被阅读0次

第一点:项目结构

启动类一定绝对不要有比他的位置还高的文件,controller包可以和他同级,不然就是404,我很烦,浪费了我一上午时间。

静态资源都在resource/static下

模板在templates下

img

第二点:Rest风格的增删改查

响应码 方法
Post
Delete
put
Get

路径中只有名词没有动词,名词为复数。

做到看到url知道是哪个版本,要用什么数据,看到状态码知道什么结果,看到请求方法知道要做什么!

@RestController = @Controller + @ResponseBody

第三点: Json

默认使用Jackon 转化。

常用注解

@JsonIgnore //排除属性不做序列化与反序列化
@JsonProperty //为属性换一个名【前后端名称不一致时使用】
@JsonPropertyOrder(value={"name1","name2"}) //改变json子属性的默认定义的顺序
@JsonInclude(JsonInclude.Include.NON_NULL) //排除为空的元素不做序列化反序列化
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") //指定属性格式

全局日期转换

spring: 
    jackson:
        date-format: yyyy-MM-dd HH:mm:ss
        time-zone: GMT+8

第四点: Mock测试【看着不太重要】

第五点: swagger2生成AIP

第一步 导包

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.6.1</version>
</dependency>

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.6.1</version>
</dependency>

第二步 配置

package com.yuan.usespringboot.swagger;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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;

/**
任意位置随便创建一个类,粘贴一下内容即可
 * @author Yuan-9826
 */
@Configuration
@EnableSwagger2
public class SwaggerUtil {
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                //扫描basePackage包下面的“/rest/”路径下的内容作为接口文档构建的目标
                .apis(RequestHandlerSelectors.basePackage("要生成文档的接口所在的包"))
                .paths(PathSelectors.regex("详细设定要生成API锁在的文件【/*.*代表所有文件】"))   
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("标题:springboot利用swagger构建api文档")
                .description("描述:简单优雅的restfun风格")
                .termsOfServiceUrl("服务路径:http://localhost:8080/api-ms.html")【这步看着没用】
                .version("版本:1.0")
                .build();
    }
}

生成位置:项目路径/swagger-ui.html

详细讲解:https://www.jianshu.com/p/3441289c7afc

第六点: 【待续】

相关文章

网友评论

      本文标题:springBoot使用

      本文链接:https://www.haomeiwen.com/subject/xvxwmctx.html