Swagger2 的配置与简单运用

一. Swagger2 配置

springboot 目录下新建 config 文件夹


 @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                //是否开启 (true 开启  false隐藏。生产环境建议隐藏)
                //.enable(false)
                .select()
                //扫描的路径包,设置basePackage会将包下的所有被@Api标记类的所有方法作为api
                .apis(RequestHandlerSelectors.basePackage("com.example.demo.controller"))
                //指定路径处理PathSelectors.any()代表所有的路径
                .paths(PathSelectors.any())
                .build();
    }
 
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                //设置文档标题(API名称)
                .title("SpringBoot中使用Swagger2接口规范")
                //文档描述
                .description("接口说明")
                //服务条款URL
                .termsOfServiceUrl("http://localhost:8080/")
                //版本号
                .version("1.0.0")
                .build();

二.Swagger2进入方法

http://localhost:8080/swagger-ui.html(8080为自己端口号)

三.简单运用

在springboot项目的xml文件中插入post配置

示例如下

    <insert id="add" parameterType="com.example.demo.test.intellect" >
        insert into intellectualproperty(companyid, `time`, kind, `name`, material, updatetime) value(#{companyid},#{time},#{kind},#{name},#{material},#{updatetime})
    </insert>

其中 intellectualproperty 为数据库表名()中为对象浏览器中各参数名

在mapper中添加

boolean add(intellect intellect);

service中对应添加

boolean add(intellect intellect);

最后在controller中添加

@PostMapping("/add")
    public boolean add(@RequestBody intellect intellect){
        return intellservice.add(intellect);

然后运行成功后进入

http://localhost:8080/swagger-ui.html(8080为端口号)

点击intell(对应项目)

点击add Post

点击Try it out

即可实现增删改查中的增(POST)

ps:应删除“id”:0,因为功能实现后会自动生成id

运行成功示例

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值