JAvaはajaxリクエストツールクラス、ステータスコードなどの情報を返す
8223 ワード
JAvaはajaxリクエストツールクラスを返します
カスタムリターンステータスコードクラス
使用方法:
BaseController
コントロールコントローラでは、BaseControllerを呼び出すtoAjax()を追加、削除、変更し、AjaxResult()を呼び出すsuccess()をクエリーします.
上記の設定エラーメッセージで
package com.example.demo.util;
import java.util.HashMap;
/**
*
*
* @author liming
*/
public class AjaxResult extends HashMap
{
private static final long serialVersionUID = 1L;
/** */
public static final String CODE_TAG = "code";
/** */
public static final String MSG_TAG = "msg";
/** */
public static final String DATA_TAG = "data";
/**
* AjaxResult , 。
*/
public AjaxResult()
{
}
/**
* AjaxResult
*
* @param code
* @param msg
*/
public AjaxResult(int code, String msg)
{
super.put(CODE_TAG, code);
super.put(MSG_TAG, msg);
}
/**
* AjaxResult
*
* @param code
* @param msg
* @param data
*/
public AjaxResult(int code, String msg, Object data)
{
super.put(CODE_TAG, code);
super.put(MSG_TAG, msg);
if (data != null)
{
super.put(DATA_TAG, data);
}
}
/**
*
*
* @return
*/
public static AjaxResult success()
{
return AjaxResult.success(" ");
}
/**
*
*
* @return
*/
public static AjaxResult success(Object data)
{
return AjaxResult.success(" ", data);
}
/**
*
*
* @param msg
* @return
*/
public static AjaxResult success(String msg)
{
return AjaxResult.success(msg, null);
}
/**
*
*
* @param msg
* @param data
* @return
*/
public static AjaxResult success(String msg, Object data)
{
return new AjaxResult(HttpStatus.SUCCESS, msg, data);
}
/**
*
*
* @return
*/
public static AjaxResult error()
{
return AjaxResult.error(" ");
}
/**
*
*
* @param msg
* @return
*/
public static AjaxResult error(String msg)
{
return AjaxResult.error(msg, null);
}
/**
*
*
* @param msg
* @param data
* @return
*/
public static AjaxResult error(String msg, Object data)
{
return new AjaxResult(HttpStatus.ERROR, msg, data);
}
/**
*
*
* @param code
* @param msg
* @return
*/
public static AjaxResult error(int code, String msg)
{
return new AjaxResult(code, msg, null);
}
}
カスタムリターンステータスコードクラス
package com.example.demo.util;
/**
*
*
* @author liming
*/
public interface HttpStatus
{
/**
*
*/
public static final int SUCCESS = 200;
/**
*
*/
public static final int CREATED = 201;
/**
*
*/
public static final int ACCEPTED = 202;
/**
* ,
*/
public static final int NO_CONTENT = 204;
/**
*
*/
public static final int MOVED_PERM = 301;
/**
*
*/
public static final int SEE_OTHER = 303;
/**
*
*/
public static final int NOT_MODIFIED = 304;
/**
* ( , )
*/
public static final int BAD_REQUEST = 400;
/**
*
*/
public static final int UNAUTHORIZED = 401;
/**
* ,
*/
public static final int FORBIDDEN = 403;
/**
* ,
*/
public static final int NOT_FOUND = 404;
/**
* http
*/
public static final int BAD_METHOD = 405;
/**
* ,
*/
public static final int CONFLICT = 409;
/**
* ,
*/
public static final int UNSUPPORTED_TYPE = 415;
/**
*
*/
public static final int ERROR = 500;
/**
*
*/
public static final int NOT_IMPLEMENTED = 501;
}
使用方法:
BaseController
package com.example.demo.controller;
import com.example.demo.util.AjaxResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* web
*
* @author liming
*/
public class BaseController {
protected final Logger logger = LoggerFactory.getLogger(BaseController.class);
/**
*
*
* @param rows
* @return
*/
protected AjaxResult toAjax(int rows) {
return rows > 0 ? AjaxResult.success() : AjaxResult.error();
}
}
コントロールコントローラでは、BaseControllerを呼び出すtoAjax()を追加、削除、変更し、AjaxResult()を呼び出すsuccess()をクエリーします.
return AjaxResult.error(HttpStatus.FORBIDDEN," , ");
上記の設定エラーメッセージで
@RestController
@RequestMapping("/system/dept")
public class SysDeptController extends BaseController
{
@Autowired
private ISysDeptService deptService;
/**
*
*/
@PreAuthorize("@ss.hasPermi('system:dept:list')")
@GetMapping("/list")
public AjaxResult list(SysDept dept)
{
List depts = deptService.selectDeptList(dept);
return AjaxResult.success(deptService.buildDeptTree(depts));
}
/**
*
*/
@PreAuthorize("@ss.hasPermi('system:dept:query')")
@GetMapping(value = "/{deptId}")
public AjaxResult getInfo(@PathVariable Long deptId)
{
return AjaxResult.success(deptService.selectDeptById(deptId));
}
/**
*
*/
@GetMapping("/treeselect")
public AjaxResult treeselect(SysDept dept)
{
List depts = deptService.selectDeptList(dept);
return AjaxResult.success(deptService.buildDeptTreeSelect(depts));
}
/**
*
*/
@GetMapping(value = "/roleDeptTreeselect/{roleId}")
public AjaxResult roleDeptTreeselect(@PathVariable("roleId") Long roleId)
{
return AjaxResult.success(deptService.selectDeptListByRoleId(roleId));
}
/**
*
*/
@PreAuthorize("@ss.hasPermi('system:dept:add')")
@Log(title = " ", businessType = BusinessType.INSERT)
@PostMapping
public AjaxResult add(@Validated @RequestBody SysDept dept)
{
if (UserConstants.NOT_UNIQUE.equals(deptService.checkDeptNameUnique(dept)))
{
return AjaxResult.error(" '" + dept.getDeptName() + "' , ");
}
dept.setCreateBy(SecurityUtils.getUsername());
return toAjax(deptService.insertDept(dept));
}
/**
*
*/
@PreAuthorize("@ss.hasPermi('system:dept:edit')")
@Log(title = " ", businessType = BusinessType.UPDATE)
@PutMapping
public AjaxResult edit(@Validated @RequestBody SysDept dept)
{
if (UserConstants.NOT_UNIQUE.equals(deptService.checkDeptNameUnique(dept)))
{
return AjaxResult.error(" '" + dept.getDeptName() + "' , ");
}
else if (dept.getParentId().equals(dept.getDeptId()))
{
return AjaxResult.error(" '" + dept.getDeptName() + "' , ");
}
dept.setUpdateBy(SecurityUtils.getUsername());
return toAjax(deptService.updateDept(dept));
}
/**
*
*/
@PreAuthorize("@ss.hasPermi('system:dept:remove')")
@Log(title = " ", businessType = BusinessType.DELETE)
@DeleteMapping("/{deptId}")
public AjaxResult remove(@PathVariable Long deptId)
{
if (deptService.hasChildByDeptId(deptId))
{
return AjaxResult.error(" , ");
}
if (deptService.checkDeptExistUser(deptId))
{
return AjaxResult.error(" , ");
}
return toAjax(deptService.deleteDeptById(deptId));
}
}