java使用原生swagger_java之swagger使用接口api

java之swagger使用接口api

java之swagger使用接口api

swagger 配置简介

我们的项目时ssm框架。我是在我们web项目测试的:

1.pom 引用

com.fasterxml.jackson.core

jackson-databind

2.9.5

io.springfox

springfox-swagger2

2.6.1

io.springfox

springfox-swagger-ui

2.6.1

经过测试,2.9.2的swagger , spring 4.0.6使用不了,需要进行升级(spring4.3.17可用)。

2.配置一些公共信息。需要新建一个类,这里是ApiConfig.java.(这个可以不用配置,swagger有默认,但是最好设置下接口的公共信息)

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.ComponentScan;

import org.springframework.context.annotation.Configuration;

import org.springframework.stereotype.Component;

import org.springframework.web.servlet.config.annotation.EnableWebMvc;

import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import springfox.documentation.builders.ApiInfoBuilder;

import springfox.documentation.builders.ParameterBuilder;

import springfox.documentation.builders.PathSelectors;

import springfox.documentation.builders.RequestHandlerSelectors;

import springfox.documentation.schema.ModelRef;

import springfox.documentation.service.ApiInfo;

import springfox.documentation.service.Parameter;

import springfox.documentation.spi.DocumentationType;

import springfox.documentation.spring.web.plugins.Docket;

import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;

import java.util.List;

/**

* @author xyd

* @description swagger使用

* @date 2019/06/08 11:09

*/

@Configuration

@EnableSwagger2

@ComponentScan(basePackages = {"com.bee.daifu.web"})

@EnableWebMvc

public class ApiConfig extends WebMvcConfigurationSupport {

@Bean

public Docket customDocket() {

/**

* 设置全局参数(不是必须)

*/

ParameterBuilder ticketPar = new ParameterBuilder();

List pars = new ArrayList();

ticketPar.name("token").description("令牌").modelRef(new ModelRef("string")).parameterType("header")

.required(false).build(); // header中的token参数非必填,传空也可以

pars.add(ticketPar.build()); // 根据每个方法名也知道当前方法在设置什么参数

/**

* 这里有包含正则

*/

return new Docket(DocumentationType.SWAGGER_2).select().

apis(RequestHandlerSelectors.basePackage("com.bee.daifu"))

.paths(PathSelectors.any())

.build()

// .globalOperationParameters(pars).加载全局参数,如果有的话

.apiInfo(apiInfo());

}

/**

* 这个是设置大标题小标题

* @return

*/

ApiInfo apiInfo() {

return new ApiInfoBuilder().title("swagger测试项目")

.description("java后端接口api文档")

.version("0.1.0")

// .termsOfServiceUrl("http://192.168.1.24:8888/swagger-ui.html")

.build();

}

}

3.配置静态访问:在spring-servlet中配置如下:

4.开始写接口,这里以TestController举例:

import com.bee.sys.utils.Result;

import io.swagger.annotations.*;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseBody;

import org.springframework.web.bind.annotation.RestController;

/**

* @author xyd

* @description swagger测试

* @date 2019/06/08 11:01

*/

@Api(tags="这是类的功能说明")

@RestController//这个会把出参自动转化为json

@RequestMapping("TestController.Controller")

public class TestController {

@ApiOperation(httpMethod = "POST", value = "方法的功能说明",response = TestDto.class)

@ApiImplicitParams({//入参说明

@ApiImplicitParam(name="openId",value="系统openId",required=true,dataType = "String"),

@ApiImplicitParam(name="name", value = "用户姓名", required = true, dataType = "String"),

@ApiImplicitParam(name="age", value = "用户年龄", required = true, dataType = "Integer")

})

@ApiResponses(value = {//我们系统的出参状态,1成功,0失败

@ApiResponse(code = 1,message = "返回调用成功"),

@ApiResponse(code = 0,message = "接口调用失败")

})

@RequestMapping("test/test")

@ResponseBody

public Result test(String name, String openId,Integer age){

System.out.println("============================================================================");

Result res = new Result();

res.setResultCode("1");

return res;

}

}

5.看下效果(注意:这个页面可以测试接口的连通性,输入参数,点击 try it out ,即可看到接口返回)

f63895b2616cc8ee6113198b483a42f9.png

5829573842c65b78cf314688aa70fafa.png

分享一个转载的:(工作自己使用的,有问题私我删)

Swagger2 的正确玩儿法 :https://mp.weixin.qq.com/s/YUNsVe9KKuGr5PY6Hs2efA

java之swagger使用接口api相关教程

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值