Swagger
- 导入依赖
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<!--swagger ui-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
- 配置文件SwaggerConfig.java
/**
* @Author Tiam
* @Date 2021/12/10 13:48
* @Description: Swagger配置类
*/
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket webApiConfig() {
return new Docket(DocumentationType.SWAGGER_2)
.groupName("webApi")
.apiInfo(webApiInfo())
.select()
// 指定controller包
.apis(RequestHandlerSelectors.basePackage("com.example"))
// 所有controller
//.paths(PathSelectors.any()) 会带有基础错误控制类
.build();
}
private ApiInfo webApiInfo() {
return new ApiInfoBuilder()
.title("Swagger2-测试API文档")
//文档作用描述
.description("本文档对平时的一些接口进行测试")
.version("1.0")
//Swagger地址
.termsOfServiceUrl("http://localhost:8080/swagger-ui.html")
//作者名,链接地址,联系方式
.contact(new Contact("Tiam", "http://www.yujing.fit/", "3036293856@qq.com"))
.build();
}
}
.paths(PathSelectors.any()) 效果
POJO类
@ApiModelProperty(value ="ID")
private Integer id;
@ApiModelProperty(value ="年龄")
private Integer age;
@ApiModelProperty(value ="姓名")
private String name;
@ApiModelProperty(value ="创建日期")
private Date createDate;
控制类
/**
* @Author Tiam
* @Date 2021/12/9 13:55
* @Description:
*/
@Api(tags = "User控制类")
@RestController
@Slf4j
public class UserController {
@Autowired
private UserService userService;
@ApiOperation(value = "根据姓名查询", notes = "备注:更详细的说明")
@ApiImplicitParam(name = "name", value = "姓名", paramType = "path")
@GetMapping("/findUserByName/name/{name}")
public List<User> findUserByName(@PathVariable("name") String name) {
log.info("您输入的姓名是:",name);
return userService.select(name);
}
}
RestTemplate类使用
配置类RestemplateConfig
/**
* @Author Tiam
* @Date 2021/12/6 10:52
* @Description: RestTemplate配置类
*/
@Configuration
public class RestemplateConfig {
/**
* 调用其他服务的接口
* @return
*/
@Bean
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
控制类
/**
* @Author Tiam
* @Date 2021/12/10 11:10
* @Description: http接口请求调用测试
*/
@Api(tags = "http请求控制类")
@RestController
@Slf4j
public class HttpController {
public static final String URL = "http://apis.juhe.cn/fapig/sudoku/generate?key=34f37a2e3ebb4b7133b54264a44c6596&difficulty=";
@Resource
private RestTemplate restTemplate;
/**
* 获取数独游戏模型
*
* @param difficulty 难度级别, easy(简单)、normal(普通)、hard(困难)、veryhard(非常困难); 默认easy
* @return
*/
@ApiOperation(value = "数独", notes = "数独游戏模型获取")
@ApiImplicitParam(name = "difficulty"
, value = "难度级别, easy(简单)、normal(普通)、hard(困难)、veryhard(非常困难); 默认easy"
, paramType = "query" //参数位置说明
, defaultValue = "easy" //默认值
,required = false
//,example = "easy(简单)、normal(普通)、hard(困难)、veryhard(非常困难); 默认easy"
)
@GetMapping(value = {"/getSudoku/dif"})
public Object getSudoku(@RequestParam(required = false)String difficulty) {
log.info("转发链接:{}", URL + difficulty);
//get请求 , url地址 , 返回数据类型
return restTemplate.getForObject(URL + difficulty, Object.class);
}
}
测试演示
参考文章
https://blog.csdn.net/jinjiniao1/article/details/100849237
https://blog.csdn.net/itguangit/article/details/78825505