微服务项目:尚融宝(8)(后端接口:积分等级CRUD)

认清现实,放弃幻想,准备斗争

一、积分等级列表接口

1、编写积分等级管理接口

在controller中添加admin包,添加AdminIntegralGradeController类

@CrossOrigin
@RestController
@RequestMapping("/admin/core/integralGrade")
public class AdminIntegralGradeController {

    @Resource
    private IntegralGradeService integralGradeService;

    @GetMapping("/list")
    public List<IntegralGrade> listAll(){
        return integralGradeService.list();
    }
}

2、测试

重启服务,访问: http://localhost:8110/admin/core/integralGrade/list 查看结果json数据

二、逻辑删除接口

1、添加删除方法

AdminIntegralGradeController添加removeById方法

​​​​​​​@DeleteMapping("/remove/{id}")
public boolean removeById(@PathVariable Long id){
    return integralGradeService.removeById(id);
}

 BUG排除

工程的pom文件变成灰色,表示忽略处理,这个玩意一般是你删掉的创建的工程不够干净,然后又重建了一个工程(同名),

 

原因:

新建的模块名与之前删除过的模块重名了,此时IDEA会认为此Project中需要排除该Module,可能就会导致pom文件变成灰色。

解决方案:

路径:Setting→Build Tools→Maven->Ignored Files ,找到被打勾忽略的Module,然后将Ignored Files中的打勾去掉即可。

三、配置Swagger2

1、Swagger2配置文件

在service-base中创建Swagger2Config

@Configuration
@EnableSwagger2
public class Swagger2Config {
    
    @Bean
    public Docket adminApiConfig(){

        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("adminApi")
                .apiInfo(adminApiInfo())
                .select()
                //只显示admin路径下的页面
                .paths(Predicates.and(PathSelectors.regex("/admin/.*")))
                .build();

    }

    private ApiInfo adminApiInfo(){

        return new ApiInfoBuilder()
                .title("尚融宝后台管理系统-API文档")
                .description("本文档描述了尚融宝后台管理系统接口")
                .version("1.0")
                .contact(new Contact("Helen", "http://atguigu.com", "55317332@qq.com"))
                .build();
    }
}

2、查看Swagger文档

重启服务器查看接口文档:http://localhost:8110/swagger-ui.html

3、常见注解

实体类注解:entity的实体类中可以添加一些自定义设置,例如:

@ApiModelProperty(value = "创建时间", example = "2019-01-01 8:00:00")
private LocalDateTime createTime;

@ApiModelProperty(value = "更新时间", example = "2019-01-01 8:00:00")
private LocalDateTime updateTime;

这个可以在swagger测试里面如果需要传jsion对象的时候默认,这俩个属性将有俩个默认的值

controller注解:

定义在类上

@Api(tags = "积分等级管理")

 定义在方法上

@ApiOperation("积分等级列表")

@ApiOperation(value = "根据id删除积分等级", notes = "逻辑删除")

定义在参数上

@ApiParam(value = "数据id", required = true, example = "100")

如果想要以不同的页面展示不同的接口文档,可以进行分组,比如想管理端的文档和web端的文档进行分开,可以进行分组处理

@Configuration
@EnableSwagger2
public class Swagger2Config {

    private ApiInfo ApiConfig(){
        return new ApiInfoBuilder().title("尚融宝后台管理系统API文档")
                .description("本文本描述了尚融宝后台管理系统的各个模块的调用方式")
                .version("1.6")
                .contact(new Contact("helen","http://atguigu.com","admin@atguigu.com"))
                .build();

    }
    @Bean
    public Docket apiConfig(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(ApiConfig())
                .groupName("adminApi").select()
                .paths(Predicates.and(PathSelectors.regex("/admin/.*")))
                .build();
    }
    
    private ApiInfo webApiConfig(){
        return new ApiInfoBuilder().title("尚融宝网站管理系统API文档")
                .description("本文本描述了尚融宝网站系统的各个模块的调用方式")
                .version("1.6")
                .contact(new Contact("helen","http://atguigu.com","admin@atguigu.com"))
                .build();

    }
    
    @Bean
    public  Docket webAopConfig()
    {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(webApiConfig())
                .groupName("webApi").select()
            .paths(Predicates.and(PathSelectors.regex("/api/.*")))
            .build();
    }


}

  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

一个风轻云淡

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

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

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

打赏作者

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

抵扣说明:

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

余额充值