一坨屎,jsonObject有bug

This commit is contained in:
Cool 2023-11-24 21:04:41 +08:00
parent 1ddc34d25e
commit 1c84d4faa4
10 changed files with 79 additions and 23 deletions

View File

@ -21,6 +21,10 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>com.auth0</groupId>
<artifactId>java-jwt</artifactId>
@ -66,9 +70,11 @@
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>2.0.32</version>
<version>1.2.83</version>
</dependency>
</dependencies>
<build>

View File

@ -1,10 +1,12 @@
package com.example.chat;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
@EnableWebSocket
//@EnableWebSocket
@Mapper
@SpringBootApplication
public class ChatApplication {

View File

@ -1,11 +1,15 @@
package com.example.chat.controller;
import com.example.chat.ChatApplication;
import com.example.chat.dao.chat.ChatMapper;
import com.example.chat.entity.Email;
import com.example.chat.entity.Result;
import com.example.chat.entity.User;
import com.example.chat.entity.chat.Message;
import com.example.chat.service.CodeCheck;
import com.example.chat.service.LoginService;
import com.example.chat.service.mail.MailUtil;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@ -22,6 +26,8 @@ public class LoginController {
@Resource
MailUtil mailUtil;
@PostMapping("/login")
public Result Login(@RequestBody User user) throws IOException {
Result result = new Result();

View File

@ -3,6 +3,7 @@ package com.example.chat.dao;
import com.example.chat.entity.User;
import com.example.chat.dao.mybatis.MybatisSingleton;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import org.springframework.stereotype.Repository;

View File

@ -7,7 +7,6 @@ import org.springframework.stereotype.Repository;
import java.io.IOException;
import java.util.List;
@Repository
public interface ChatMapper {
public abstract void addMessage(Message message) throws IOException;

View File

@ -7,7 +7,6 @@ import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Repository;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@Repository
@ -28,10 +27,11 @@ public class ChatMessage implements ChatMapper {
sqlSession.close();
}
@Override
public List<Message> getMessage() throws IOException {
List<Message> messageList;
SqlSession sqlSession = MybatisSingleton.getSqlSessionFactory().openSession();
messageList = sqlSession.selectOne("dao.chat.Message.getMessage");
messageList = sqlSession.selectList("dao.chat.Message.getMessage");
sqlSession.close();
return messageList;
}

View File

@ -0,0 +1,34 @@
package com.example.chat.dao.chat;
import com.example.chat.ChatApplication;
import com.example.chat.dao.UserMapper;
import com.example.chat.entity.User;
import com.example.chat.entity.chat.Message;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
import java.io.IOException;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = ChatApplication.class)
public class MapperTest {
@Resource(name = "chatMessage")
ChatMapper mapper;
@Resource(name = "login")
UserMapper userMapper;
@Test
public void Test02() throws IOException {
System.out.println(userMapper.getUser(new User("1", "1", "1", "1")));
}
@Test
public void Test01() throws IOException {
mapper.addMessage(new Message("1", "1", "1", "1"));
}
}

View File

@ -24,6 +24,7 @@ public class InterceptorConfig implements WebMvcConfigurer {
excludePath.add("/sendCode");
excludePath.add("/register");//注册
excludePath.add("/register");
excludePath.add("/chat/get");
registry.addInterceptor(tokenInterceptor)//注册拦截器
.addPathPatterns("/**")//拦截所有请求
.excludePathPatterns(excludePath);//添加拦截白名单

View File

@ -4,6 +4,7 @@ import com.example.chat.dao.UserMapper;
import com.example.chat.dao.redis.RedisUtil;
import com.example.chat.entity.User;
import com.example.chat.jwt.JWTUtil;
import org.junit.Test;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@ -17,14 +18,17 @@ public class LoginService {
@Resource(name = "tokenRedis")
RedisUtil redisUtil;
@Test
public void Test01() throws IOException {
System.out.println(userTable.getUser(new User()));
}
public String LoginCheck(User requestUser) throws IOException {
User user;
user = userTable.getUser(requestUser);
System.out.println(user);
System.out.println(requestUser);
if (user != null) {
if (user.getPassword().equals(requestUser.getPassword())) {
String token = JWTUtil.sign(user.getAccount());
redisUtil.addRedis(user.getAccount(), token);

View File

@ -1,35 +1,37 @@
package com.example.chat.service.websocket;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.chat.dao.chat.ChatMapper;
import com.example.chat.dao.chat.ChatMessage;
import com.example.chat.entity.chat.Message;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.swing.*;
import javax.websocket.*;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
@CrossOrigin
@Slf4j
@ServerEndpoint(value = "/chat/get")
@Service
@Component
public class WebSocketServer {
private final List<Session> sessionList = new ArrayList<>();
private static final List<Session> sessionList = new CopyOnWriteArrayList<>();
@Resource
ChatMapper chatMapper;
public ChatMapper chatMapper=new ChatMessage();
@OnOpen
public void onOpen(Session session) throws IOException {
sessionList.add(session);
log.info("有新用户加入聊天,当前在线人数为{}", sessionList.size());
if (chatMapper == null) {
log.error("chatMapper为null");
}
List<Message> messageList = chatMapper.getMessage();
sendAllSession(messageList);
}
@ -41,6 +43,7 @@ public class WebSocketServer {
List<Message> messageList = chatMapper.getMessage();
sendAllSession(messageList);
}
@OnError
public void onError(Throwable error) {
log.error("发生错误");
@ -61,7 +64,7 @@ public class WebSocketServer {
try {
for (Session session : sessionList) {
log.info("给客户端发送消息{}", session);
session.getBasicRemote().sendObject(message);
session.getBasicRemote().sendText(JSON.toJSONString(message));
}
} catch (Exception e) {
log.error("消息发送失败", e);