springboot的swagger配置与SSM稍微有些不同,SSM对2.9.0以上的兼容性很差,但是springboot就可以使用2.9.0以上的包了,其实区别不算太大,除了能对对象直接操作外就是页面更清爽了。
目录
4、启动效果:【http://127.0.0.1:8088/swagger-ui.html】
<!-- swagger包这里2.9.2 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
这里单独创建了一个包【com.item.swagger】来放置swagger的配置文件
需要注意的是:【com.item.controller】这里需要改成自己的包位置。
package com.item.swagger;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
public class SwaggerConfig {
private static Logger log = LoggerFactory.getLogger(SwaggerConfig.class);
@Bean
public Docket createRestApi() {
log.info("进入到swagger的配置中");
return new Docket(DocumentationType.SWAGGER_2)
// 指定构建api文档的详细信息的方法:apiInfo()
.apiInfo(apiInfo())
.groupName("红目香薰")
.select()
// 指定要生成api接口的包路径,这里把controller作为包路径,生成controller中的所有接口
.apis(RequestHandlerSelectors.basePackage("com.item.controller"))
.paths(PathSelectors.any())
.build();
}
/**
* 构建api文档的详细信息
* @return
*/
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
// 设置页面标题
.title("Spring Boot集成Swagger2接口总览")
// 设置接口描述
.description("Swagger接口")
// 设置联系方式
.contact(new Contact("测试swagger","http://localhost:8080/",""))
// 设置版本
.version("1.0")
// 构建
.build();
}
}
我写了一套的注释方法,一目了然
package com.item.controller;
import com.item.model.Users;
import com.item.service.UsersService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
@Api("用户操作接口")
@RestController
@CrossOrigin
public class UsersController {
@Autowired
private UsersService usersService;
/**
* 获取所有信息
* @return
*/
@GetMapping("/GetInfoApi")
@ApiOperation(value = "获取信息",notes = "没啥留言的")
public Object GetInfoApi(){
List<Users> list=usersService.GetInfo();
HashMap<String,Object> map=new HashMap<String,Object>();
map.put("state",true);
map.put("msg","成功");
map.put("result",list);
return map;
}
@GetMapping("/GetName")
@ApiOperation(value = "获取信息",notes = "没啥留言的")
@ApiImplicitParams({
@ApiImplicitParam(name = "nickName",required = true,paramType = "query",dataType = "String",value = "通过昵称模糊查询")
})
public Object GetName(HttpServletRequest request,Model model){
String nickName = request.getParameter("nickName");
List<Users> list=usersService.SelectName(nickName);
HashMap<String,Object> map=new HashMap<String,Object>();
map.put("state",true);
map.put("msg","成功");
map.put("result",list);
return map;
}
/**
* 添加信息
* @param userName
* @param pwd
* @param nickName
* @return
*/
@PostMapping(value = "/UserAddInfoApi")
@ApiOperation(value = "添加",notes = "没啥留言的")
@ApiImplicitParams({
@ApiImplicitParam(name = "userName",required = true,paramType = "query",dataType = "String",value = "用户名"),
@ApiImplicitParam(name = "pwd",required = true,paramType = "query",dataType = "String",value = "密码"),
@ApiImplicitParam(name = "nickName",required = true,paramType = "query",dataType = "String",value = "昵称")
})
public Object UserAddInfoApi(String userName,String pwd,String nickName){
HashMap<String,Object> map=new HashMap<String,Object>();
if(
StringUtils.isEmpty(userName)||
StringUtils.isEmpty(pwd)||
StringUtils.isEmpty(nickName)
){
map.put("state",false);
map.put("msg","参数不润许为空");
map.put("result","");
return map;
}
usersService.UsersAddInfo(userName, pwd, nickName);
map.put("state",true);
map.put("msg","成功");
map.put("result","");
return map;
}
/**
* 单个查询
* @param id
* @return
*/
@GetMapping("/UsersSelectById")
@ApiOperation(value = "id查询",notes = "没啥留言的")
@ApiImplicitParams({
@ApiImplicitParam(name = "id",required = true,paramType = "query",dataType = "String",value = "编号")
})
public Object UsersSelectById(String id){
Users users = usersService.UsersSelectById(Integer.parseInt(id));
HashMap<String,Object> map=new HashMap<String,Object>();
map.put("state",true);
map.put("msg","成功");
map.put("result",users);
return map;
}
/**
* 修改api
* @param id
* @param pwd
* @return
*/
@PostMapping(value = "/UserUpdateInfoApi")
@ApiOperation(value = "添加",notes = "没啥留言的")
@ApiImplicitParams({
@ApiImplicitParam(name = "id",required = true,paramType = "query",dataType = "String",value = "编号"),
@ApiImplicitParam(name = "pwd",required = true,paramType = "query",dataType = "String",value = "密码"),
})
public Object UserUpdateInfoApi(String id,String pwd){
usersService.UsersUpdateInfo(pwd,Integer.parseInt(id));
HashMap<String,Object> map=new HashMap<String,Object>();
map.put("state",true);
map.put("msg","成功");
map.put("result","");
return map;
}
/**
* 删除api
* @param id
* @return
*/
@GetMapping(value = "/UsersDeleteById")
@ApiOperation(value = "根据id删除",notes = "没啥留言的")
@ApiImplicitParams({
@ApiImplicitParam(name = "id",required = true,paramType = "query",dataType = "String",value = "编号")
})
public Object UsersDeleteById(String id){
usersService.UsersDeleteById(Integer.parseInt(id));
HashMap<String,Object> map=new HashMap<String,Object>();
map.put("state",true);
map.put("msg","成功");
map.put("result","");
return map;
}
}
这里为了看着方便,我将服务路径改为了【/】
# 服务路径
server.servlet.context-path=/
POST的也类似
1、SwaggerConfig的配置文件中忘记写注解,就2个注解:
@Configuration
@EnableSwagger2
2、接口中的注解:
@Api("用户操作接口")
@ApiOperation(value = "获取信息",notes = "没啥留言的")
@ApiImplicitParams({
@ApiImplicitParam(name = "nickName",required = true,paramType = "query",dataType = "String",value = "通过昵称模糊查询")
})
3、没有明确接口的访问类型,导致出现一堆的同名不同访问类型的接口提示。
使用@GetMapping或者@PostMapping就可以解决此问题。
希望能给大家带来帮助。