在spring里任何位置拿到当前请求的对象

  public static ServletRequestAttributes getRequestAttributes()
    {
        RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
        return (ServletRequestAttributes) attributes;
    }

    (ServletRequestAttributes) attributes.getRequest;


拿到容器里的对象的方式

package com.qf.fmall.websocket;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.qf.fmall.entity.Candidate;
import com.qf.fmall.mapper.CandidateMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.*;

@Component
@ServerEndpoint("/test/{random}")
@Slf4j
public class CandidateSocketServer implements ApplicationContextAware {

  public static Map<Double,Session> map= new HashMap<Double,Session>();

//  @Autowired
//  private CandidateMapper candidateMapper;
    private static ApplicationContext applicationContext;


    @OnOpen
    public void onOpen(Session session, @PathParam("random")Double random) throws IOException, InterruptedException {
    
        System.out.println("连接成功");
        log.info("传进来的id={}",random);
        map.put(random,session);
    }


   @OnMessage
    public  void  onMessage(Session session,String msg) throws IOException {
        ArrayList<Candidate> candidates = new ArrayList<>();

       CandidateMapper candidateMapper = applicationContext.getBean(CandidateMapper.class);
    
       Candidate c1 = candidateMapper.selectOne(new QueryWrapper<Candidate>().eq("id", 1));
    
        Candidate c2 = candidateMapper.selectOne(new QueryWrapper<Candidate>().eq("id", 2));
    
        candidates.add(c1);
        candidates.add(c2);
    
       ObjectMapper mapper = new ObjectMapper();
       String json = mapper.writeValueAsString(candidates);
    
       session.getBasicRemote().sendText(json);

   }



    @OnClose
    public void onClose(Session session) throws IOException, InterruptedException {
    
        session.close();
    
    }


    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        CandidateSocketServer.applicationContext = applicationContext;
    }

}


  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值