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


      public static ServletRequestAttributes getRequestAttributes()
        {
            RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
            return (ServletRequestAttributes) attributes;
        }
    
        (ServletRequestAttributes) attributes.getRequest;
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    拿到容器里的对象的方式

    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;
        }
    
    }
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
  • 相关阅读:
    7.1 yolov5优化模型时,自动标注xml数据
    Qt编写4K/8K大分辨率播放器(8K占用1%CPU)
    Java版本+企业电子招投标系统源代码+支持二开+招投标系统+中小型企业采购供应商招投标平台
    软件测试(功能、工具、接口、性能、自动化、测开)详解
    WebView输入框软键盘遮挡问题(沉浸状态栏和adjustResize的冲突)
    MSDC 4.3 接口规范(1)
    携创教育:自考本科要考哪些科目?自考专升本有什么优势?
    硬件设计评审,让我心态崩了
    微服务·架构组件之网关
    Elasticsearch:什么是检索增强生成 - RAG?
  • 原文地址:https://blog.csdn.net/qq_53374893/article/details/132856528