SpringBoot 封裝返回類以及session 添加獲取

一、建立返回類Resultsession

public class Result<T>{
    /*錯誤碼*/
    private Integer code;

    /*提示信息 */
    private String msg;

    /*具體內容*/
    private  T data;

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

}

二、建立 ResultUtil 返回對象app

public class ResultUtil {

    /**
     * 請求成功返回
     * @param object
     * @return
     */
    public static Result success(Object object){
        Result Result=new Result();
        Result.setCode(200);
        Result.setMsg("請求成功");
        Result.setData(object);
        return Result;
    }
    public static Result success(){
        return success(null);
    }

    public static Result error(Integer code,String resultResult){
        Result Result=new Result();
        Result.setCode(code);
        Result.setMsg(resultResult);
        return Result;
    }
}

三、調用返回類this

@RequestMapping("/link")
@RestController
public class LinkController {

    @Autowired
    private LinkRepository linkRepository;

    @PostMapping(value = "/list")
    @ResponseBody public Result getlinklList() { return ResultUtil.success(linkRepository.findAll()); } }

到此步 完成簡單的返回類封裝spa

 

四、session 的實現code

 public String hel(HttpSession request,Model model) {
        request.setAttribute("name","lisi"); //設置session值
        String name = (String) request.getAttribute("name"); //獲取session值
        model.addAttribute("name",name);
        return "index";
    }
相關文章
相關標籤/搜索