Session trong Java

@RequestMapping(value = "/call-detail", method = RequestMethod.POST)
@ResponseBody
public String makeCall(
        @PathVariable(value = "appointmentId") String appointmentId,
        HttpServletRequest req) {
    SimpleObject callDetails = getCallDetails(appointmentId, req);
    try {
        Thread.sleep(20000);
    } catch (InterruptedException e) {
        System.err.format("IOException: %s%n", e);
    }
    return CallService.makeCall(callDetails);
}

MN cho mình hỏi có cách nào lấy đc session mà k thông qua parameter đc truyền mở method xuống k? k lấy từ thằng HttpServletRequest req mà có cách nào lấy ngay trong body của method k

Nếu cậu sử dụng spring, cậu thử sử dụng RequestContextHolder xem.

HttpSession session = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes().getRequest().getSession(true);
4 Likes
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?