swagger2的使用(swagger如何使用)

swagger2的使用(swagger如何使用)program jpademo description EmployeeCont author ZengGuangfu create 2018 10 23 11 07 RestControll RequestMappi emp Api value 用户管理类

大家好,我是讯享网,很高兴认识大家。



/
讯享网
  • @program: jpademo
  • @description: EmployeeController
  • @author: ZengGuangfu
  • @create 2018-10-23 11:07 */ ​ @RestController @RequestMapping(“emp”) @Api(value = “用户管理类”) public class EmployeeController { ​ @Autowired private EmployeeReposiroty employeeReposiroty; ​ /
    讯享网 * 增加人物 * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> employee * </span><span style="color: rgba(128, 128, 128, 1)">@return</span> 
    */ @PostMapping(value = “employee”) @ApiOperation(value = “新增一个用户”,notes = “新增之后返回对象”) @ApiImplicitParam(paramType = “query”,name = “employee”,value = “用户”,required = true) @ApiResponse(code = 400,message = “参数没有填好”,response = String.class) public String insert(Employee employee){
     Employee employee1 </span>=<span style="color: rgba(0, 0, 0, 1)"> employeeReposiroty.save(employee); </span><span style="color: rgba(0, 0, 255, 1)">if</span>(employee1 != <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">) { </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> SysNode.Judge.SUCCESS.getResult(); }</span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> { </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> SysNode.Judge.FAILD.getResult(); } 
    } ​
    /
    讯享网 * 删除单个用户 * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> id * </span><span style="color: rgba(128, 128, 128, 1)">@return</span> 
    */ @DeleteMapping(value = “employee/{id}”) @ApiOperation(value = “删除用户”,notes = “根据成员id删除单个用户”) @ApiImplicitParam(paramType = “path”,name = “id”,value = “用户id”,required = true,dataType = “Integer”) @ApiResponse(code = 400,message = “参数没有填好”,response = String.class) public String delete(@PathVariable(“id”)Integer id){
     </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)">{ employeeReposiroty.deleteById(id); </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> SysNode.Judge.SUCCESS.getResult(); }</span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (Exception e){ e.printStackTrace(); </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> SysNode.Judge.FAILD.getResult(); } 
    } ​
    /
    讯享网 * 修改单个成员 * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> employee * </span><span style="color: rgba(128, 128, 128, 1)">@return</span> 
    */ @PutMapping(value = “employee/{id}”) @ApiOperation(value = “修改用户信息”,notes = “根据成员id修改单个用户”) public String update(Employee employee){
     </span><span style="color: rgba(0, 128, 0, 1)">/</span><span style="color: rgba(0, 128, 0, 1)"> * save方法如果参数属性缺失,会导致原本存在的数据为null </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)"> Employee employee1 </span>=<span style="color: rgba(0, 0, 0, 1)"> employeeReposiroty.saveAndFlush(employee); </span><span style="color: rgba(0, 0, 255, 1)">if</span> (employee1 != <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">) { </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> SysNode.Judge.SUCCESS.getResult(); }</span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> { </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> SysNode.Judge.FAILD.getResult(); } 
    } ​
    /
    讯享网 * 获取所有成员,升序排列 * </span><span style="color: rgba(128, 128, 128, 1)">@return</span> 
    */ @GetMapping(value = “employee/sort”) @ApiOperation(value = “查询全部用户”,notes = “默认根据升序查询全部用户信息”) public List&lt;Employee&gt; findAll(){
     Sort orders </span>= <span style="color: rgba(0, 0, 255, 1)">new</span> Sort(Sort.Direction.DESC,"employeeId"<span style="color: rgba(0, 0, 0, 1)">); List</span>&lt;Employee&gt; employeeList =<span style="color: rgba(0, 0, 0, 1)"> employeeReposiroty.findAll(orders); </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> employeeList; 
    } ​
    /
    • 获取所有成员,升序排列
    • @return */ @GetMapping(value = “employee/pageSort”) @ApiOperation(value = “查询用户信息”,notes = “查询用户信息”) @ApiImplicitParams({ @ApiImplicitParam(paramType = “query”,name = “sort”,value = “排序方式:asc|desc”,dataType = “String”,required = true), @ApiImplicitParam(paramType = “query”,name = “pagenumber”,value = “第几页”,dataType = “Integer”,required = true), @ApiImplicitParam(paramType = “query”,name = “pageSize”,value = “分页数”,dataType = “Integer”,required = true) }) public List&lt;Employee&gt; findAllByPage(String sort,Integer pagenumber,Integer pageSize){ try { Sort.Direction sortlast; if(“desc”.equals(sort.toLowerCase())){ sortlast = Sort.Direction.DESC; }else{
      sortlast
      = Sort.Direction.ASC; } Sort orders = new Sort(sortlast, “employeeId”); Pageable pageable = new PageRequest(pagenumber, pageSize, orders); ​ Page&lt;Employee&gt; employeePage = employeeReposiroty.findAll(pageable); List&lt;Employee&gt; employeeList = employeePage.getContent(); return employeeList; }catch (Exception e){ e.printStackTrace(); return null; } } /
    • 自定义拓展jpa,根据用户名查找单个用户
    • @param username
    • @return */ @GetMapping(value = “employee/find/{username}”) @ApiOperation(value = “查询用户信息”,notes = “根据用户登录名查询该用户信息”) @ApiImplicitParam(paramType = “path”,name = “username”,value = “用户登录名”,required = true,dataType = “String”) public Employee findByUsername(@PathVariable(“username”) String username){ List&lt;Employee&gt; employeeList = employeeReposiroty.findByUserNameOrderByEmployeeIdAsc(username); if (employeeList != null && !employeeList.isEmpty()){ return employeeList.get(0); } return null; } ​ /
    • 测试用
    • @return */ @GetMapping(value = “employee/grade”) public List&lt;Object[]&gt; findEmployeeAndGrade(){ Pageable pageable = new PageRequest(0,3); ​ Page&lt;Object[]&gt; page = employeeReposiroty.findEmployeeAndGrade(pageable); System.out.println(page.getTotalElements()+“———-结果总数————”); System.out.println(page.getTotalPages()+“——–根据pageSize的总页数———–”); System.out.println(page.getNumber()+“——–当前页数,pageNumber———-”); System.out.println(page.getNumberOfElements()+“——–当前页有几个数据——–”); System.out.println(page.getSize()+“———PageSize————-”); System.out.println(page.getSort()+“———排序方式,没有则是’UNSORTED’———-”); ​ List&lt;Object[]&gt; objects = page.getContent(); return objects; } }

讯享网
小讯
上一篇 2025-04-26 22:41
下一篇 2025-04-19 19:53

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/195654.html