上项目的下载地址 http://download.csdn.NET/detail/cai750415222/9871446
项目结构,狠明确的三层结构
首先构建一个maven的工程
做好pox.xml的jar的引入,然后在swagger的配置
因为swagger是一个API的原始引用,所有可以直接访问页面,当配置好springboot和swagger之后,
再浏览器中直接输入 http://localhost:8080/swagger-ui.html 就可以访问
写好一定的方法,在做测试的时候我们可以直接用swagger提供的json测试
例如:查询
直接输入ID 点击查询
我们可以看到很详细的查询结果和返回值,这样就省去了在URL中输入参数查询的麻烦,
其他的删除,新增都是一样的
@RestController
@RequestMapping(value="/users")
public class UserController {
@Autowired
private IUserService userService;
@ApiOperation(value="获取用户列表", notes="")
@RequestMapping(value={""}, method=RequestMethod.GET)
public List getUserList() {
List r = userService.getUserList();
return r;
}
/*
@ApiOperation(value = “接口说明”, httpMethod = “接口请求方式”,
response = “接口返回参数类型”, notes = “接口发布说明”;
@ApiParam(required = “是否必须参数”, name = “参数名称”, value = “参数具体描述”
*/
@ApiOperation(value="创建用户",notes="根据User对象创建用户")
@ApiImplicitParam(required = true ,name="user",value = "用户详细实体user", dataType = "User")
@RequestMapping(value="", method=RequestMethod.POST)
public Integer postUser(@RequestBody User user){
return userService.insert(user);
}
@ApiOperation(value="获取用户详细信息",notes="根据url中的ID参数来获取用户的详细信息")
@RequestMapping(value="getById/{id}",method=RequestMethod.GET)
public User getUserById(@ApiParam(value = "用户ID", required = true) @PathVariable Long id){
return userService.getUserById(id);
}
@ApiOperation(value="更新用户详细信息",notes="根据URL中ID的参数值来指定对象,并根据USER传过来的参数信息来现实更新")
@RequestMapping(value="updateById/{userId}",method=RequestMethod.PUT)
public Integer updateUser(
@ApiParam(value = "用户ID", required = true) @PathVariable(value = "userId") Long userId,
@ApiParam(value = "用户详细实体user", required = true) @RequestBody User user ){
return userService.updateUser(userId, user);
}
@ApiOperation(value="第二种更新用户详细信息所有信息放在一个Bean中",notes="根据URL中ID的参数值来指定对象,并根据USER传过来的参数信息来现实更新")
@RequestMapping(value="updateById2/{userId}",method=RequestMethod.PUT)
public Integer updateUser2(
@ApiParam(value = "用户详细实体user", required = true) @RequestBody User user ){
return userService.updateUser2(user);
}
@ApiOperation(value="删除用户", notes="根据url的id来指定删除对象")
@RequestMapping(value="deleteUserById/{Id}", method=RequestMethod.DELETE)
public Integer deleteUserById(@ApiParam(value = "用户ID", required = true) @PathVariable(value = "Id")Long id){
return userService.deleteUserById(id);
}
}
下面我们来说说swa里面的一些注解和标签
@ApiOperation(value="创建用户",notes="根据User对象创建用户")
@ApiImplicitParam(required = true ,name="user",value = "用户详细实体user", dataType = "User")
@RequestMapping(value="", method=RequestMethod.POST)
public Integer postUser(@RequestBody User user){
return userService.insert(user);
}
@ApiOperation(value="获取用户详细信息",notes="根据url中的ID参数来获取用户的详细信息")
@RequestMapping(value="getById/{id}",method=RequestMethod.GET)
public User getUserById(@ApiParam(value = "用户ID", required = true) @PathVariable Long id){
return userService.getUserById(id);
}
@ApiOperation(value="更新用户详细信息",notes="根据URL中ID的参数值来指定对象,并根据USER传过来的参数信息来现实更新")
@RequestMapping(value="updateById/{userId}",method=RequestMethod.PUT)
public Integer updateUser(
@ApiParam(value = "用户ID", required = true) @PathVariable(value = "userId") Long userId,
@ApiParam(value = "用户详细实体user", required = true) @RequestBody User user ){
return userService.updateUser(userId, user);
}
这个是其中的一些方法
@ApiOperation(value = “接口说明”, httpMethod = “接口请求方式”,
response = “接口返回参数类型”, notes = “接口发布说明”;
@ApiParam(required = “是否必须参数”, name = “参数名称”, value = “参数具体描述”
@ApiImplicitParam是全局的 类似于@ApiParam 但是区别于@ApiImplicitParams
@RequestMapping 我就不说了
最后附上项目的下载地址 http://download.csdn.net/detail/cai750415222/9871446