一pom.xml
<dependencies>
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
</dependencies>
二 config
package main.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@Configuration
public class WebMvcConfigurer extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
}
}
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.mischen.it.controller"))
//这个是所在的controller,要自己修改
.paths(PathSelectors.any())
.build();
}
//配置在线文档的基本信息
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("springboot利用swagger构建api文档")
.description("简单优雅的restfun风格,https://me.csdn.net/blog/miachen520")
.termsOfServiceUrl("https://me.csdn.net/blog/miachen520")
.version("1.0")
.build();
}
}
三 启动类备注 @EnableSwagger2
四 创建接口
@Api("客服接口")
@Controller
@RequestMapping("/checkcenter")
public class CheckCenterController {
// 创建线程安全的Map
static Map<Integer, User> users = Collections.synchronizedMap(new HashMap<Integer, User>());
@ApiOperation(value="测试接口api", notes="根据cid获取客服信息,this is test api")
@ApiImplicitParam(name = "cid", value = "客户id", required = true, dataType = "String")
@ResponseBody
@PostMapping("/getCenter/{cid}")
public ResponseEntity<RusultObject> getCinterCall(@PathVariable String cid) {
RusultObject ret=new RusultObject();
Map<String,String> map=new LinkedHashMap<String,String>();
map.put("cid",cid);
map.put("name","客服");
ret.setResult(map);
ret.setStatus("ok");
return new ResponseEntity<RusultObject>(ret, HttpStatus.OK);
}
/**
* 查询用户列表
* @return
*/
@ApiOperation(value="获取用户列表", notes="获取用户列表")
@RequestMapping(value = "users", method = RequestMethod.GET)
public ResponseEntity<JsonResult> getUserList (){
JsonResult r = new JsonResult();
try {
//List<User> userList = new ArrayList<User>(users.values());
Map<String,String> map=new LinkedHashMap<String,String>();
map.put("aa", "小红");
map.put("bb", "小明");
r.setResult(map);
r.setStatus("ok");
} catch (Exception e) {
r.setResult(e.getClass().getName() + ":" + e.getMessage());
r.setStatus("error");
e.printStackTrace();
}
return ResponseEntity.ok(r);
}
}
四
启动服务8081,打开http://localhost:8081/swagger-ui.html
二pagehelper
1pom.xml
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.10</version>
</dependency>
2直接使用
@Override
public ServerResponse selectAll() {
PageHelper.startPage(1,2);
List<test>list = dao.selectAll();
PageInfo<test>pageInfo=new PageInfo<>(list);
return ServerResponse.createBySuccess("检查成功",pageInfo);
}