From 4484b8d2afbe5090aac73e690183bb43ec2e1e83 Mon Sep 17 00:00:00 2001 From: luoxiang <2806718453@qq.com> Date: Mon, 31 Dec 2018 18:51:18 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E7=94=A8=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- spring-boot/spring-boot-jsp/pom.xml | 58 +++++++++++++++ .../springboot/SpringBootJspApplication.java | 14 ++++ .../springboot/bean/Programmer.java | 24 +++++++ .../springboot/controller/JspController.java | 24 +++++++ .../src/main/resources/application.yml | 5 ++ .../src/main/webapp/WEB-INF/jsp/show.jsp | 13 ++++ .../src/main/webapp/css/show.css | 4 ++ .../SpringBootJspApplicationTests.java | 17 +++++ spring-boot/spring-boot-mongodb/pom.xml | 48 +++++++++++++ .../SpringBootMongodbApplication.java | 14 ++++ .../springboot/bean/Programmer.java | 26 +++++++ .../repository/ProgrammerRepository.java | 18 +++++ .../src/main/resources/application.yml | 5 ++ .../heibaiying/springboot/MongoJPATests.java | 72 +++++++++++++++++++ .../springboot/MongoOriginalTests.java | 68 ++++++++++++++++++ spring-boot/spring-boot-redis/pom.xml | 55 ++++++++++++++ .../SpringBootRedisApplication.java | 14 ++++ .../springbootredis/bean/Programmer.java | 28 ++++++++ .../springbootredis/config/RedisConfig.java | 43 +++++++++++ .../redis/RedisObjectOperation.java | 70 ++++++++++++++++++ .../springbootredis/redis/RedisOperation.java | 70 ++++++++++++++++++ .../src/main/resources/application.yml | 9 +++ .../springbootredis/RedisObjectTests.java | 57 +++++++++++++++ .../springbootredis/RedisTests.java | 47 ++++++++++++ spring-boot/spring-boot-tomcat/pom.xml | 63 ++++++++++++++++ .../SpringBootTomcatApplication.java | 28 ++++++++ .../springboottomcat/bean/Programmer.java | 24 +++++++ .../controller/JspController.java | 24 +++++++ .../src/main/resources/application.yml | 5 ++ .../src/main/webapp/WEB-INF/jsp/show.jsp | 13 ++++ .../src/main/webapp/css/show.css | 4 ++ .../SpringBootTomcatApplicationTests.java | 17 +++++ spring-boot/spring-boot-websocket/pom.xml | 56 +++++++++++++++ .../SpringBootWebsocketApplication.java | 14 ++++ .../springboot/constant/Constant.java | 17 +++++ .../controller/LoginController.java | 28 ++++++++ .../springboot/websocket/ChatSocket.java | 51 +++++++++++++ .../springboot/websocket/WebSocketConfig.java | 17 +++++ .../src/main/resources/application.properties | 0 .../src/main/resources/templates/chat.ftl | 39 ++++++++++ .../src/main/resources/templates/index.ftl | 12 ++++ .../SpringBootWebsocketApplicationTests.java | 17 +++++ .../heibaiying/websocket/CustomerHandler.java | 1 + 43 files changed, 1233 insertions(+) create mode 100644 spring-boot/spring-boot-jsp/pom.xml create mode 100644 spring-boot/spring-boot-jsp/src/main/java/com/heibaiying/springboot/SpringBootJspApplication.java create mode 100644 spring-boot/spring-boot-jsp/src/main/java/com/heibaiying/springboot/bean/Programmer.java create mode 100644 spring-boot/spring-boot-jsp/src/main/java/com/heibaiying/springboot/controller/JspController.java create mode 100644 spring-boot/spring-boot-jsp/src/main/resources/application.yml create mode 100644 spring-boot/spring-boot-jsp/src/main/webapp/WEB-INF/jsp/show.jsp create mode 100644 spring-boot/spring-boot-jsp/src/main/webapp/css/show.css create mode 100644 spring-boot/spring-boot-jsp/src/test/java/com/heibaiying/springboot/SpringBootJspApplicationTests.java create mode 100644 spring-boot/spring-boot-mongodb/pom.xml create mode 100644 spring-boot/spring-boot-mongodb/src/main/java/com/heibaiying/springboot/SpringBootMongodbApplication.java create mode 100644 spring-boot/spring-boot-mongodb/src/main/java/com/heibaiying/springboot/bean/Programmer.java create mode 100644 spring-boot/spring-boot-mongodb/src/main/java/com/heibaiying/springboot/repository/ProgrammerRepository.java create mode 100644 spring-boot/spring-boot-mongodb/src/main/resources/application.yml create mode 100644 spring-boot/spring-boot-mongodb/src/test/java/com/heibaiying/springboot/MongoJPATests.java create mode 100644 spring-boot/spring-boot-mongodb/src/test/java/com/heibaiying/springboot/MongoOriginalTests.java create mode 100644 spring-boot/spring-boot-redis/pom.xml create mode 100644 spring-boot/spring-boot-redis/src/main/java/com/heibaiying/springbootredis/SpringBootRedisApplication.java create mode 100644 spring-boot/spring-boot-redis/src/main/java/com/heibaiying/springbootredis/bean/Programmer.java create mode 100644 spring-boot/spring-boot-redis/src/main/java/com/heibaiying/springbootredis/config/RedisConfig.java create mode 100644 spring-boot/spring-boot-redis/src/main/java/com/heibaiying/springbootredis/redis/RedisObjectOperation.java create mode 100644 spring-boot/spring-boot-redis/src/main/java/com/heibaiying/springbootredis/redis/RedisOperation.java create mode 100644 spring-boot/spring-boot-redis/src/main/resources/application.yml create mode 100644 spring-boot/spring-boot-redis/src/test/java/com/heibaiying/springbootredis/RedisObjectTests.java create mode 100644 spring-boot/spring-boot-redis/src/test/java/com/heibaiying/springbootredis/RedisTests.java create mode 100644 spring-boot/spring-boot-tomcat/pom.xml create mode 100644 spring-boot/spring-boot-tomcat/src/main/java/com/heibaiying/springboottomcat/SpringBootTomcatApplication.java create mode 100644 spring-boot/spring-boot-tomcat/src/main/java/com/heibaiying/springboottomcat/bean/Programmer.java create mode 100644 spring-boot/spring-boot-tomcat/src/main/java/com/heibaiying/springboottomcat/controller/JspController.java create mode 100644 spring-boot/spring-boot-tomcat/src/main/resources/application.yml create mode 100644 spring-boot/spring-boot-tomcat/src/main/webapp/WEB-INF/jsp/show.jsp create mode 100644 spring-boot/spring-boot-tomcat/src/main/webapp/css/show.css create mode 100644 spring-boot/spring-boot-tomcat/src/test/java/com/heibaiying/springboottomcat/SpringBootTomcatApplicationTests.java create mode 100644 spring-boot/spring-boot-websocket/pom.xml create mode 100644 spring-boot/spring-boot-websocket/src/main/java/com/heibaiying/springboot/SpringBootWebsocketApplication.java create mode 100644 spring-boot/spring-boot-websocket/src/main/java/com/heibaiying/springboot/constant/Constant.java create mode 100644 spring-boot/spring-boot-websocket/src/main/java/com/heibaiying/springboot/controller/LoginController.java create mode 100644 spring-boot/spring-boot-websocket/src/main/java/com/heibaiying/springboot/websocket/ChatSocket.java create mode 100644 spring-boot/spring-boot-websocket/src/main/java/com/heibaiying/springboot/websocket/WebSocketConfig.java create mode 100644 spring-boot/spring-boot-websocket/src/main/resources/application.properties create mode 100644 spring-boot/spring-boot-websocket/src/main/resources/templates/chat.ftl create mode 100644 spring-boot/spring-boot-websocket/src/main/resources/templates/index.ftl create mode 100644 spring-boot/spring-boot-websocket/src/test/java/com/heibaiying/springboot/SpringBootWebsocketApplicationTests.java diff --git a/spring-boot/spring-boot-jsp/pom.xml b/spring-boot/spring-boot-jsp/pom.xml new file mode 100644 index 0000000..07b7ad7 --- /dev/null +++ b/spring-boot/spring-boot-jsp/pom.xml @@ -0,0 +1,58 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.1.1.RELEASE + + + com.heibaiying + spring-boot-jsp + 0.0.1-SNAPSHOT + spring-boot-jsp + JSP project for Spring Boot + + + 1.8 + + + + + org.springframework.boot + spring-boot-starter-web + + + + org.projectlombok + lombok + true + + + org.springframework.boot + spring-boot-starter-test + test + + + + org.apache.tomcat.embed + tomcat-embed-jasper + provided + + + javax.servlet + jstl + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/spring-boot/spring-boot-jsp/src/main/java/com/heibaiying/springboot/SpringBootJspApplication.java b/spring-boot/spring-boot-jsp/src/main/java/com/heibaiying/springboot/SpringBootJspApplication.java new file mode 100644 index 0000000..9fa00cd --- /dev/null +++ b/spring-boot/spring-boot-jsp/src/main/java/com/heibaiying/springboot/SpringBootJspApplication.java @@ -0,0 +1,14 @@ +package com.heibaiying.springboot; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class SpringBootJspApplication { + + public static void main(String[] args) { + SpringApplication.run(SpringBootJspApplication.class, args); + } + +} + diff --git a/spring-boot/spring-boot-jsp/src/main/java/com/heibaiying/springboot/bean/Programmer.java b/spring-boot/spring-boot-jsp/src/main/java/com/heibaiying/springboot/bean/Programmer.java new file mode 100644 index 0000000..58d1b70 --- /dev/null +++ b/spring-boot/spring-boot-jsp/src/main/java/com/heibaiying/springboot/bean/Programmer.java @@ -0,0 +1,24 @@ +package com.heibaiying.springboot.bean; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.time.LocalDate; + +/** + * @author : heibaiying + */ +@Data +@AllArgsConstructor +@NoArgsConstructor +public class Programmer { + + private String name; + + private int age; + + private float salary; + + private LocalDate birthday; +} diff --git a/spring-boot/spring-boot-jsp/src/main/java/com/heibaiying/springboot/controller/JspController.java b/spring-boot/spring-boot-jsp/src/main/java/com/heibaiying/springboot/controller/JspController.java new file mode 100644 index 0000000..92b2dc1 --- /dev/null +++ b/spring-boot/spring-boot-jsp/src/main/java/com/heibaiying/springboot/controller/JspController.java @@ -0,0 +1,24 @@ +package com.heibaiying.springboot.controller; + +import com.heibaiying.springboot.bean.Programmer; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.RequestMapping; + +import java.time.LocalDate; + +/** + * @author : heibaiying + */ + +@Controller +@RequestMapping("index") +public class JspController { + + @RequestMapping + public String jsp(Model model){ + Programmer programmer = new Programmer("heibai", 21, 1298.31f, LocalDate.now()); + model.addAttribute("programmer",programmer); + return "show"; + } +} diff --git a/spring-boot/spring-boot-jsp/src/main/resources/application.yml b/spring-boot/spring-boot-jsp/src/main/resources/application.yml new file mode 100644 index 0000000..77489aa --- /dev/null +++ b/spring-boot/spring-boot-jsp/src/main/resources/application.yml @@ -0,0 +1,5 @@ +spring: + mvc: + view: + prefix: /WEB-INF/jsp/ + suffix: .jsp \ No newline at end of file diff --git a/spring-boot/spring-boot-jsp/src/main/webapp/WEB-INF/jsp/show.jsp b/spring-boot/spring-boot-jsp/src/main/webapp/WEB-INF/jsp/show.jsp new file mode 100644 index 0000000..cd0f50b --- /dev/null +++ b/spring-boot/spring-boot-jsp/src/main/webapp/WEB-INF/jsp/show.jsp @@ -0,0 +1,13 @@ +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + programmer + + + + + + \ No newline at end of file diff --git a/spring-boot/spring-boot-jsp/src/main/webapp/css/show.css b/spring-boot/spring-boot-jsp/src/main/webapp/css/show.css new file mode 100644 index 0000000..c5ef712 --- /dev/null +++ b/spring-boot/spring-boot-jsp/src/main/webapp/css/show.css @@ -0,0 +1,4 @@ +li{ + font-size: 30px; + color: blueviolet; +} \ No newline at end of file diff --git a/spring-boot/spring-boot-jsp/src/test/java/com/heibaiying/springboot/SpringBootJspApplicationTests.java b/spring-boot/spring-boot-jsp/src/test/java/com/heibaiying/springboot/SpringBootJspApplicationTests.java new file mode 100644 index 0000000..54bbb8d --- /dev/null +++ b/spring-boot/spring-boot-jsp/src/test/java/com/heibaiying/springboot/SpringBootJspApplicationTests.java @@ -0,0 +1,17 @@ +package com.heibaiying.springboot; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@SpringBootTest +public class SpringBootJspApplicationTests { + + @Test + public void contextLoads() { + } + +} + diff --git a/spring-boot/spring-boot-mongodb/pom.xml b/spring-boot/spring-boot-mongodb/pom.xml new file mode 100644 index 0000000..2989e79 --- /dev/null +++ b/spring-boot/spring-boot-mongodb/pom.xml @@ -0,0 +1,48 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.1.1.RELEASE + + + com.heibaiying + spring-boot-mongodb + 0.0.1-SNAPSHOT + spring-boot-mongodb + mongodb demo project for Spring Boot + + + 1.8 + + + + + org.springframework.boot + spring-boot-starter-data-mongodb + + + + org.springframework.boot + spring-boot-starter-test + test + + + org.projectlombok + lombok + true + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/spring-boot/spring-boot-mongodb/src/main/java/com/heibaiying/springboot/SpringBootMongodbApplication.java b/spring-boot/spring-boot-mongodb/src/main/java/com/heibaiying/springboot/SpringBootMongodbApplication.java new file mode 100644 index 0000000..68906d9 --- /dev/null +++ b/spring-boot/spring-boot-mongodb/src/main/java/com/heibaiying/springboot/SpringBootMongodbApplication.java @@ -0,0 +1,14 @@ +package com.heibaiying.springboot; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class SpringBootMongodbApplication { + + public static void main(String[] args) { + SpringApplication.run(SpringBootMongodbApplication.class, args); + } + +} + diff --git a/spring-boot/spring-boot-mongodb/src/main/java/com/heibaiying/springboot/bean/Programmer.java b/spring-boot/spring-boot-mongodb/src/main/java/com/heibaiying/springboot/bean/Programmer.java new file mode 100644 index 0000000..74f3019 --- /dev/null +++ b/spring-boot/spring-boot-mongodb/src/main/java/com/heibaiying/springboot/bean/Programmer.java @@ -0,0 +1,26 @@ +package com.heibaiying.springboot.bean; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; +import org.springframework.data.annotation.Id; + +import java.util.Date; + +/** + * @author : heibaiying + */ +@Data +@AllArgsConstructor +@NoArgsConstructor +public class Programmer { + + @Id + private String name; + + private int age; + + private float salary; + + private Date birthday; +} diff --git a/spring-boot/spring-boot-mongodb/src/main/java/com/heibaiying/springboot/repository/ProgrammerRepository.java b/spring-boot/spring-boot-mongodb/src/main/java/com/heibaiying/springboot/repository/ProgrammerRepository.java new file mode 100644 index 0000000..48ffe18 --- /dev/null +++ b/spring-boot/spring-boot-mongodb/src/main/java/com/heibaiying/springboot/repository/ProgrammerRepository.java @@ -0,0 +1,18 @@ +package com.heibaiying.springboot.repository; + +import com.heibaiying.springboot.bean.Programmer; +import org.springframework.data.mongodb.repository.MongoRepository; + +/** + * @author : heibaiying + * @description : jpa 方式查询 对于mongo而言 更推荐这种查询方式 比起原生的语法更加简洁 + */ +public interface ProgrammerRepository extends MongoRepository { + + void deleteAllByName(String name); + + Programmer findAllByName(String names); + + Programmer findByNameAndAge(String name, int age); + +} diff --git a/spring-boot/spring-boot-mongodb/src/main/resources/application.yml b/spring-boot/spring-boot-mongodb/src/main/resources/application.yml new file mode 100644 index 0000000..c88dfa5 --- /dev/null +++ b/spring-boot/spring-boot-mongodb/src/main/resources/application.yml @@ -0,0 +1,5 @@ +spring: + data: + mongodb: + database: spring + uri: mongodb://192.168.0.108:27017 \ No newline at end of file diff --git a/spring-boot/spring-boot-mongodb/src/test/java/com/heibaiying/springboot/MongoJPATests.java b/spring-boot/spring-boot-mongodb/src/test/java/com/heibaiying/springboot/MongoJPATests.java new file mode 100644 index 0000000..4809050 --- /dev/null +++ b/spring-boot/spring-boot-mongodb/src/test/java/com/heibaiying/springboot/MongoJPATests.java @@ -0,0 +1,72 @@ +package com.heibaiying.springboot; + +import com.heibaiying.springboot.bean.Programmer; +import com.heibaiying.springboot.repository.ProgrammerRepository; +import com.mongodb.client.result.DeleteResult; +import com.mongodb.client.result.UpdateResult; +import org.junit.Assert; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.data.domain.Page; +import org.springframework.data.mongodb.core.MongoTemplate; +import org.springframework.data.mongodb.core.query.Criteria; +import org.springframework.data.mongodb.core.query.Query; +import org.springframework.test.context.junit4.SpringRunner; + +import java.util.ArrayList; +import java.util.Date; +import java.util.List; +import java.util.Optional; + +import static org.springframework.data.mongodb.core.query.Criteria.where; +import static org.springframework.data.mongodb.core.query.Query.query; +import static org.springframework.data.mongodb.core.query.Update.update; + +@RunWith(SpringRunner.class) +@SpringBootTest +public class MongoJPATests { + + @Autowired + private ProgrammerRepository repository; + + @Test + public void insert() { + // 单条插入 + repository.save(new Programmer("python", 23, 21832.34f, new Date())); + // 批量插入 + List programmers = new ArrayList(); + programmers.add(new Programmer("java", 21, 52200.21f, new Date())); + programmers.add(new Programmer("Go", 34, 500.21f, new Date())); + repository.saveAll(programmers); + } + + // 条件查询 + @Test + public void select() { + Programmer java = repository.findByNameAndAge("java", 21); + Assert.assertEquals(java.getSalary(), 52200.21f, 0.01); + } + + + // 更新数据 + @Test + public void MUpdate() { + repository.save(new Programmer("Go", 8, 500.21f, new Date())); + Programmer go = repository.findAllByName("Go"); + Assert.assertEquals(go.getAge(), 8); + } + + // 删除指定数据 + @Test + public void delete() { + repository.deleteAllByName("python"); + Optional python = repository.findById("python"); + Assert.assertFalse(python.isPresent()); + } + +} + diff --git a/spring-boot/spring-boot-mongodb/src/test/java/com/heibaiying/springboot/MongoOriginalTests.java b/spring-boot/spring-boot-mongodb/src/test/java/com/heibaiying/springboot/MongoOriginalTests.java new file mode 100644 index 0000000..68b90d1 --- /dev/null +++ b/spring-boot/spring-boot-mongodb/src/test/java/com/heibaiying/springboot/MongoOriginalTests.java @@ -0,0 +1,68 @@ +package com.heibaiying.springboot; + +import com.heibaiying.springboot.bean.Programmer; +import com.mongodb.client.result.DeleteResult; +import com.mongodb.client.result.UpdateResult; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.data.mongodb.core.MongoTemplate; +import org.springframework.data.mongodb.core.query.Criteria; +import org.springframework.data.mongodb.core.query.Query; +import org.springframework.test.context.junit4.SpringRunner; + +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +import static org.springframework.data.mongodb.core.query.Criteria.where; +import static org.springframework.data.mongodb.core.query.Query.query; +import static org.springframework.data.mongodb.core.query.Update.update; + +@RunWith(SpringRunner.class) +@SpringBootTest +public class MongoOriginalTests { + + @Autowired + private MongoTemplate mongoTemplate; + + @Test + public void insert() { + // 单条插入 + mongoTemplate.insert(new Programmer("xiaoming", 12, 5000.21f, new Date())); + List programmers = new ArrayList(); + // 批量插入 + programmers.add(new Programmer("xiaohong", 21, 52200.21f, new Date())); + programmers.add(new Programmer("xiaolan", 34, 500.21f, new Date())); + mongoTemplate.insert(programmers, Programmer.class); + } + + // 条件查询 + @Test + public void select() { + Criteria criteria = new Criteria(); + criteria.andOperator(where("name").is("xiaohong"), where("age").is(21)); + Query query = new Query(criteria); + Programmer one = mongoTemplate.findOne(query, Programmer.class); + System.out.println(one); + } + + + // 更新数据 + @Test + public void MUpdate() { + UpdateResult updateResult = mongoTemplate.updateMulti(query(where("name").is("xiaoming")), update("age", 35), Programmer.class); + System.out.println("更新记录数:" + updateResult.getModifiedCount()); + } + + // 删除指定数据 + @Test + public void delete() { + DeleteResult result = mongoTemplate.remove(query(where("name").is("xiaolan")), Programmer.class); + System.out.println("影响记录数:" + result.getDeletedCount()); + System.out.println("是否成功:" + result.wasAcknowledged()); + } + +} + diff --git a/spring-boot/spring-boot-redis/pom.xml b/spring-boot/spring-boot-redis/pom.xml new file mode 100644 index 0000000..4a9bde7 --- /dev/null +++ b/spring-boot/spring-boot-redis/pom.xml @@ -0,0 +1,55 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.1.1.RELEASE + + + com.heibaiying + spring-boot-redis + 0.0.1-SNAPSHOT + spring-boot-redis + Redis project for Spring Boot + + + 1.8 + + + + + + org.springframework.boot + spring-boot-starter-data-redis + + + + org.projectlombok + lombok + true + + + org.springframework.boot + spring-boot-starter-test + test + + + + com.fasterxml.jackson.core + jackson-databind + 2.9.8 + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/spring-boot/spring-boot-redis/src/main/java/com/heibaiying/springbootredis/SpringBootRedisApplication.java b/spring-boot/spring-boot-redis/src/main/java/com/heibaiying/springbootredis/SpringBootRedisApplication.java new file mode 100644 index 0000000..a71eff4 --- /dev/null +++ b/spring-boot/spring-boot-redis/src/main/java/com/heibaiying/springbootredis/SpringBootRedisApplication.java @@ -0,0 +1,14 @@ +package com.heibaiying.springbootredis; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class SpringBootRedisApplication { + + public static void main(String[] args) { + SpringApplication.run(SpringBootRedisApplication.class, args); + } + +} + diff --git a/spring-boot/spring-boot-redis/src/main/java/com/heibaiying/springbootredis/bean/Programmer.java b/spring-boot/spring-boot-redis/src/main/java/com/heibaiying/springbootredis/bean/Programmer.java new file mode 100644 index 0000000..a106070 --- /dev/null +++ b/spring-boot/spring-boot-redis/src/main/java/com/heibaiying/springbootredis/bean/Programmer.java @@ -0,0 +1,28 @@ +package com.heibaiying.springbootredis.bean; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.NoArgsConstructor; + +import java.io.Serializable; +import java.time.LocalDate; +import java.util.Date; + +/** + * @author : heibaiying + */ +@Data +@AllArgsConstructor +@NoArgsConstructor +@EqualsAndHashCode +public class Programmer implements Serializable { + + private String name; + + private int age; + + private float salary; + + private Date birthday; +} \ No newline at end of file diff --git a/spring-boot/spring-boot-redis/src/main/java/com/heibaiying/springbootredis/config/RedisConfig.java b/spring-boot/spring-boot-redis/src/main/java/com/heibaiying/springbootredis/config/RedisConfig.java new file mode 100644 index 0000000..12ebd0d --- /dev/null +++ b/spring-boot/spring-boot-redis/src/main/java/com/heibaiying/springbootredis/config/RedisConfig.java @@ -0,0 +1,43 @@ +package com.heibaiying.springbootredis.config; + +import com.fasterxml.jackson.annotation.JsonAutoDetect; +import com.fasterxml.jackson.annotation.PropertyAccessor; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.redis.connection.RedisConnectionFactory; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; +import org.springframework.data.redis.serializer.StringRedisSerializer; + +/** + * @author : heibaiying + * @description : 自定义序列化器 + * 不定义的话默认采用的是serializer.JdkSerializationRedisSerializer.serialize()序列化为二进制字节码 存储在数据库中不直观 + */ +@Configuration +public class RedisConfig { + + @Bean + public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory) { + RedisTemplate redisTemplate = new RedisTemplate<>(); + redisTemplate.setConnectionFactory(redisConnectionFactory); + + // 使用Jackson2JsonRedisSerialize 需要导入依赖 com.fasterxml.jackson.core jackson-databind + Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); + + ObjectMapper objectMapper = new ObjectMapper(); + // 第一个参数表示: 表示所有访问者都受到影响 包括 字段, getter / isGetter,setter,creator + // 第二个参数表示: 所有类型的访问修饰符都是可接受的,不论是公有还有私有表变量都会被序列化 + objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); + objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); + + jackson2JsonRedisSerializer.setObjectMapper(objectMapper); + + // 设置key,value 序列化规则 + redisTemplate.setValueSerializer(jackson2JsonRedisSerializer); + redisTemplate.setKeySerializer(new StringRedisSerializer()); + redisTemplate.afterPropertiesSet(); + return redisTemplate; + } +} diff --git a/spring-boot/spring-boot-redis/src/main/java/com/heibaiying/springbootredis/redis/RedisObjectOperation.java b/spring-boot/spring-boot-redis/src/main/java/com/heibaiying/springbootredis/redis/RedisObjectOperation.java new file mode 100644 index 0000000..f40d0a7 --- /dev/null +++ b/spring-boot/spring-boot-redis/src/main/java/com/heibaiying/springbootredis/redis/RedisObjectOperation.java @@ -0,0 +1,70 @@ +package com.heibaiying.springbootredis.redis; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.redis.core.*; +import org.springframework.stereotype.Component; + +import java.util.List; +import java.util.Set; +import java.util.concurrent.TimeUnit; + +/** + * @author : heibaiying + * @description : redis 基本操作 + */ + +@Component +public class RedisObjectOperation { + + @Autowired + private RedisTemplate objectRedisTemplate; + + /*** + * 操作对象 + */ + public void ObjectSet(Object key, Object value) { + ValueOperations valueOperations = objectRedisTemplate.opsForValue(); + valueOperations.set(key, value); + } + + /*** + * 操作元素为对象列表 + */ + public void ListSet(Object key, List values) { + ListOperations listOperations = objectRedisTemplate.opsForList(); + values.forEach(value -> listOperations.leftPush(key, value)); + } + + /*** + * 操作元素为对象集合 + */ + public void SetSet(Object key, Set values) { + SetOperations setOperations = objectRedisTemplate.opsForSet(); + values.forEach(value -> setOperations.add(key, value)); + } + + /*** + * 获取对象 + */ + public Object ObjectGet(Object key) { + ValueOperations valueOperations = objectRedisTemplate.opsForValue(); + return valueOperations.get(key); + } + + /*** + * 列表弹出元素 + */ + public Object ListLeftPop(Object key) { + ListOperations listOperations = objectRedisTemplate.opsForList(); + return listOperations.leftPop(key, 2, TimeUnit.SECONDS); + } + + /*** + * 集合弹出元素 + */ + public Object SetPop(Object key) { + SetOperations setOperations = objectRedisTemplate.opsForSet(); + return setOperations.pop(key); + } + +} diff --git a/spring-boot/spring-boot-redis/src/main/java/com/heibaiying/springbootredis/redis/RedisOperation.java b/spring-boot/spring-boot-redis/src/main/java/com/heibaiying/springbootredis/redis/RedisOperation.java new file mode 100644 index 0000000..f9e605c --- /dev/null +++ b/spring-boot/spring-boot-redis/src/main/java/com/heibaiying/springbootredis/redis/RedisOperation.java @@ -0,0 +1,70 @@ +package com.heibaiying.springbootredis.redis; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.redis.core.*; +import org.springframework.stereotype.Component; + +import java.util.List; +import java.util.Set; +import java.util.concurrent.TimeUnit; + +/** + * @author : heibaiying + * @description : redis 基本操作 + */ + +@Component +public class RedisOperation { + + @Autowired + private StringRedisTemplate redisTemplate; + + /*** + * 操作普通字符串 + */ + public void StringSet(String key, String value) { + ValueOperations valueOperations = redisTemplate.opsForValue(); + valueOperations.set(key, value); + } + + /*** + * 操作列表 + */ + public void ListSet(String key, List values) { + ListOperations listOperations = redisTemplate.opsForList(); + values.forEach(value -> listOperations.leftPush(key, value)); + } + + /*** + * 操作集合 + */ + public void SetSet(String key, Set values) { + SetOperations setOperations = redisTemplate.opsForSet(); + values.forEach(value -> setOperations.add(key, value)); + } + + /*** + * 获取字符串 + */ + public String StringGet(String key) { + ValueOperations valueOperations = redisTemplate.opsForValue(); + return valueOperations.get(key); + } + + /*** + * 列表弹出元素 + */ + public String ListLeftPop(String key) { + ListOperations listOperations = redisTemplate.opsForList(); + return listOperations.leftPop(key, 2, TimeUnit.SECONDS); + } + + /*** + * 集合弹出元素 + */ + public String SetPop(String key) { + SetOperations setOperations = redisTemplate.opsForSet(); + return setOperations.pop(key); + } + +} diff --git a/spring-boot/spring-boot-redis/src/main/resources/application.yml b/spring-boot/spring-boot-redis/src/main/resources/application.yml new file mode 100644 index 0000000..a69b90f --- /dev/null +++ b/spring-boot/spring-boot-redis/src/main/resources/application.yml @@ -0,0 +1,9 @@ +spring: + redis: + host: 127.0.0.1 + port: 6379 + # ĬϲõҲ 0 ݿ redisٷ4.0֮汾ͲƼõڵݿ(db1-db15)ķʽ洢ݣҪӦòüȺʽ + database: 0 + +# ǼȺڵ ָڵ +#spring.redis.cluster.nodes \ No newline at end of file diff --git a/spring-boot/spring-boot-redis/src/test/java/com/heibaiying/springbootredis/RedisObjectTests.java b/spring-boot/spring-boot-redis/src/test/java/com/heibaiying/springbootredis/RedisObjectTests.java new file mode 100644 index 0000000..58be9ab --- /dev/null +++ b/spring-boot/spring-boot-redis/src/test/java/com/heibaiying/springbootredis/RedisObjectTests.java @@ -0,0 +1,57 @@ +package com.heibaiying.springbootredis; + +import com.heibaiying.springbootredis.bean.Programmer; +import com.heibaiying.springbootredis.redis.RedisObjectOperation; +import com.heibaiying.springbootredis.redis.RedisOperation; +import org.assertj.core.util.Sets; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +import java.time.LocalDate; +import java.util.Arrays; +import java.util.Date; + +@RunWith(SpringRunner.class) +@SpringBootTest +public class RedisObjectTests { + + @Autowired + private RedisObjectOperation redisOperation; + + @Test + public void StringOperation() { + Programmer programmer = new Programmer("小明", 12, 3334.3f, new Date()); + redisOperation.ObjectSet("programmer", programmer); + Programmer objectGet = (Programmer) redisOperation.ObjectGet("programmer"); + Assert.assertEquals(objectGet, programmer); + } + + @Test + public void ListOperation() { + Programmer programmer01 = new Programmer("小明", 12, 3334.3f, new Date()); + Programmer programmer02 = new Programmer("小红", 12, 3334.3f, new Date()); + redisOperation.ListSet("programmerList", Arrays.asList(programmer01, programmer02)); + Programmer programmer = (Programmer) redisOperation.ListLeftPop("programmerList"); + Assert.assertEquals(programmer.getName(), "小红"); + } + + /* + * 需要注意的是Redis的集合(set)不仅不允许有重复元素,并且集合中的元素是无序的, + * 不能通过索引下标获取元素。哪怕你在java中传入的集合是有序的newLinkedHashSet,但是实际在Redis存储的还是无序的集合 + */ + @Test + public void SetOperation() { + Programmer programmer01 = new Programmer("小明", 12, 3334.3f, new Date()); + Programmer programmer02 = new Programmer("小爱", 12, 3334.3f, new Date()); + Programmer programmer03 = new Programmer("小红", 12, 3334.3f, new Date()); + redisOperation.SetSet("programmerSet", Sets.newLinkedHashSet(programmer01, programmer02, programmer03)); + Programmer programmer = (Programmer) redisOperation.SetPop("programmerSet"); + Assert.assertNotNull(programmer); + } + +} + diff --git a/spring-boot/spring-boot-redis/src/test/java/com/heibaiying/springbootredis/RedisTests.java b/spring-boot/spring-boot-redis/src/test/java/com/heibaiying/springbootredis/RedisTests.java new file mode 100644 index 0000000..fa13868 --- /dev/null +++ b/spring-boot/spring-boot-redis/src/test/java/com/heibaiying/springbootredis/RedisTests.java @@ -0,0 +1,47 @@ +package com.heibaiying.springbootredis; + +import com.heibaiying.springbootredis.redis.RedisOperation; +import org.assertj.core.util.Sets; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +import java.util.Arrays; + +@RunWith(SpringRunner.class) +@SpringBootTest +public class RedisTests { + + @Autowired + private RedisOperation redisOperation; + + @Test + public void StringOperation() { + redisOperation.StringSet("hello", "redis"); + String s = redisOperation.StringGet("hello"); + Assert.assertEquals(s, "redis"); + } + + @Test + public void ListOperation() { + redisOperation.ListSet("skill", Arrays.asList("java", "oracle", "vue")); + String s = redisOperation.ListLeftPop("skill"); + Assert.assertEquals(s, "vue"); + } + + /* + * 需要注意的是Redis的集合(set)不仅不允许有重复元素,并且集合中的元素是无序的, + * 不能通过索引下标获取元素。哪怕你在java中传入的集合是有序的newLinkedHashSet,但是实际在Redis存储的还是无序的集合 + */ + @Test + public void SetOperation() { + redisOperation.SetSet("skillSet", Sets.newLinkedHashSet("java", "oracle", "vue")); + String s = redisOperation.SetPop("skillSet"); + Assert.assertNotNull(s); + } + +} + diff --git a/spring-boot/spring-boot-tomcat/pom.xml b/spring-boot/spring-boot-tomcat/pom.xml new file mode 100644 index 0000000..ea47c91 --- /dev/null +++ b/spring-boot/spring-boot-tomcat/pom.xml @@ -0,0 +1,63 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.1.1.RELEASE + + + com.heibaiying + spring-boot-tomcat + 0.0.1-SNAPSHOT + spring-boot-tomcat + boot-tomcat project for Spring Boot + + + 1.8 + + + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-tomcat + + + + + + javax.servlet + servlet-api + 2.5 + provided + + + + org.springframework.boot + spring-boot-starter-test + test + + + org.projectlombok + lombok + true + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/spring-boot/spring-boot-tomcat/src/main/java/com/heibaiying/springboottomcat/SpringBootTomcatApplication.java b/spring-boot/spring-boot-tomcat/src/main/java/com/heibaiying/springboottomcat/SpringBootTomcatApplication.java new file mode 100644 index 0000000..2be2601 --- /dev/null +++ b/spring-boot/spring-boot-tomcat/src/main/java/com/heibaiying/springboottomcat/SpringBootTomcatApplication.java @@ -0,0 +1,28 @@ +package com.heibaiying.springboottomcat; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.builder.SpringApplicationBuilder; +import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; + + +/** + * 如果用外置tomcat,启动报错java.lang.NoClassDefFoundError: javax/el/ELManager + * 是因为tomcat 7.0 el-api包中没有ELManager类 , 切换tomcat 为8.0 以上版本即可 + */ +@SpringBootApplication +public class SpringBootTomcatApplication extends SpringBootServletInitializer { + + + @Override + protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { + //传入SpringBoot应用的主程序 + return application.sources(SpringBootTomcatApplication.class); + } + + public static void main(String[] args) { + SpringApplication.run(SpringBootTomcatApplication.class, args); + } + +} + diff --git a/spring-boot/spring-boot-tomcat/src/main/java/com/heibaiying/springboottomcat/bean/Programmer.java b/spring-boot/spring-boot-tomcat/src/main/java/com/heibaiying/springboottomcat/bean/Programmer.java new file mode 100644 index 0000000..741bb19 --- /dev/null +++ b/spring-boot/spring-boot-tomcat/src/main/java/com/heibaiying/springboottomcat/bean/Programmer.java @@ -0,0 +1,24 @@ +package com.heibaiying.springboottomcat.bean; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.time.LocalDate; + +/** + * @author : heibaiying + */ +@Data +@AllArgsConstructor +@NoArgsConstructor +public class Programmer { + + private String name; + + private int age; + + private float salary; + + private LocalDate birthday; +} diff --git a/spring-boot/spring-boot-tomcat/src/main/java/com/heibaiying/springboottomcat/controller/JspController.java b/spring-boot/spring-boot-tomcat/src/main/java/com/heibaiying/springboottomcat/controller/JspController.java new file mode 100644 index 0000000..983e145 --- /dev/null +++ b/spring-boot/spring-boot-tomcat/src/main/java/com/heibaiying/springboottomcat/controller/JspController.java @@ -0,0 +1,24 @@ +package com.heibaiying.springboottomcat.controller; +; +import com.heibaiying.springboottomcat.bean.Programmer; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.RequestMapping; + +import java.time.LocalDate; + +/** + * @author : heibaiying + */ + +@Controller +@RequestMapping("index") +public class JspController { + + @RequestMapping + public String jsp(Model model){ + Programmer programmer = new Programmer("heibai", 21, 1298.31f, LocalDate.now()); + model.addAttribute("programmer",programmer); + return "show"; + } +} diff --git a/spring-boot/spring-boot-tomcat/src/main/resources/application.yml b/spring-boot/spring-boot-tomcat/src/main/resources/application.yml new file mode 100644 index 0000000..77489aa --- /dev/null +++ b/spring-boot/spring-boot-tomcat/src/main/resources/application.yml @@ -0,0 +1,5 @@ +spring: + mvc: + view: + prefix: /WEB-INF/jsp/ + suffix: .jsp \ No newline at end of file diff --git a/spring-boot/spring-boot-tomcat/src/main/webapp/WEB-INF/jsp/show.jsp b/spring-boot/spring-boot-tomcat/src/main/webapp/WEB-INF/jsp/show.jsp new file mode 100644 index 0000000..cd0f50b --- /dev/null +++ b/spring-boot/spring-boot-tomcat/src/main/webapp/WEB-INF/jsp/show.jsp @@ -0,0 +1,13 @@ +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + programmer + + + +
    +
  • 姓名: ${programmer.name}
  • +
  • 年龄: ${programmer.age}
  • +
+ + \ No newline at end of file diff --git a/spring-boot/spring-boot-tomcat/src/main/webapp/css/show.css b/spring-boot/spring-boot-tomcat/src/main/webapp/css/show.css new file mode 100644 index 0000000..c5ef712 --- /dev/null +++ b/spring-boot/spring-boot-tomcat/src/main/webapp/css/show.css @@ -0,0 +1,4 @@ +li{ + font-size: 30px; + color: blueviolet; +} \ No newline at end of file diff --git a/spring-boot/spring-boot-tomcat/src/test/java/com/heibaiying/springboottomcat/SpringBootTomcatApplicationTests.java b/spring-boot/spring-boot-tomcat/src/test/java/com/heibaiying/springboottomcat/SpringBootTomcatApplicationTests.java new file mode 100644 index 0000000..eddce28 --- /dev/null +++ b/spring-boot/spring-boot-tomcat/src/test/java/com/heibaiying/springboottomcat/SpringBootTomcatApplicationTests.java @@ -0,0 +1,17 @@ +package com.heibaiying.springboottomcat; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@SpringBootTest +public class SpringBootTomcatApplicationTests { + + @Test + public void contextLoads() { + } + +} + diff --git a/spring-boot/spring-boot-websocket/pom.xml b/spring-boot/spring-boot-websocket/pom.xml new file mode 100644 index 0000000..455dd1b --- /dev/null +++ b/spring-boot/spring-boot-websocket/pom.xml @@ -0,0 +1,56 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.1.1.RELEASE + + + com.heibaiying + spring-boot-websocket + 0.0.1-SNAPSHOT + spring-boot-websocket + Websocket project for Spring Boot + + + 1.8 + + + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-websocket + + + org.springframework.boot + spring-boot-starter-freemarker + + + + org.projectlombok + lombok + true + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/spring-boot/spring-boot-websocket/src/main/java/com/heibaiying/springboot/SpringBootWebsocketApplication.java b/spring-boot/spring-boot-websocket/src/main/java/com/heibaiying/springboot/SpringBootWebsocketApplication.java new file mode 100644 index 0000000..59c38ef --- /dev/null +++ b/spring-boot/spring-boot-websocket/src/main/java/com/heibaiying/springboot/SpringBootWebsocketApplication.java @@ -0,0 +1,14 @@ +package com.heibaiying.springboot; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class SpringBootWebsocketApplication { + + public static void main(String[] args) { + SpringApplication.run(SpringBootWebsocketApplication.class, args); + } + +} + diff --git a/spring-boot/spring-boot-websocket/src/main/java/com/heibaiying/springboot/constant/Constant.java b/spring-boot/spring-boot-websocket/src/main/java/com/heibaiying/springboot/constant/Constant.java new file mode 100644 index 0000000..4a3b0a8 --- /dev/null +++ b/spring-boot/spring-boot-websocket/src/main/java/com/heibaiying/springboot/constant/Constant.java @@ -0,0 +1,17 @@ +package com.heibaiying.springboot.constant; + +import javax.websocket.Session; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +/** + * @author : 罗祥 + * @description : + * @date :create in 2018/12/27 + */ +public interface Constant { + + String USER_NAME="username"; + + Map nameAndSession = new ConcurrentHashMap<>(); +} diff --git a/spring-boot/spring-boot-websocket/src/main/java/com/heibaiying/springboot/controller/LoginController.java b/spring-boot/spring-boot-websocket/src/main/java/com/heibaiying/springboot/controller/LoginController.java new file mode 100644 index 0000000..582497f --- /dev/null +++ b/spring-boot/spring-boot-websocket/src/main/java/com/heibaiying/springboot/controller/LoginController.java @@ -0,0 +1,28 @@ +package com.heibaiying.springboot.controller; + +import com.heibaiying.springboot.constant.Constant; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; + +import javax.servlet.http.HttpSession; + +/** + * @author : 罗祥 + * @description : 简单登录 + * @date :create in 2018/12/27 + */ +@Controller +public class LoginController { + + @PostMapping("login") + public String login(String username, HttpSession session) { + session.setAttribute(Constant.USER_NAME, username); + return "chat"; + } + + @GetMapping + public String index() { + return "index"; + } +} diff --git a/spring-boot/spring-boot-websocket/src/main/java/com/heibaiying/springboot/websocket/ChatSocket.java b/spring-boot/spring-boot-websocket/src/main/java/com/heibaiying/springboot/websocket/ChatSocket.java new file mode 100644 index 0000000..4080827 --- /dev/null +++ b/spring-boot/spring-boot-websocket/src/main/java/com/heibaiying/springboot/websocket/ChatSocket.java @@ -0,0 +1,51 @@ +package com.heibaiying.springboot.websocket; + +import com.heibaiying.springboot.constant.Constant; +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.io.UnsupportedEncodingException; +import java.net.URLDecoder; + +/** + * @author : heibaiying + */ + +@ServerEndpoint(value = "/socket/{username}") +@Component +public class ChatSocket { + + + @OnOpen //建立连接时候触发 + public void onOpen(Session session, @PathParam("username") String username) { + // 这个方法是线程不安全的 + Constant.nameAndSession.putIfAbsent(username, session); + } + + + @OnClose //关闭连接时候触发 + public void onClose(Session session, @PathParam("username") String username) { + Constant.nameAndSession.remove(username); + } + + @OnMessage //处理消息 + public void onMessage(Session session, String message, @PathParam("username") String username) throws UnsupportedEncodingException { + // 防止中文乱码 + String msg = URLDecoder.decode(message, "utf-8"); + // 简单模拟群发消息 + Constant.nameAndSession.forEach((s, webSocketSession) + -> { + try { + webSocketSession.getBasicRemote().sendText(username + " : " + msg); + } catch (IOException e) { + e.printStackTrace(); + } + }); + } +} diff --git a/spring-boot/spring-boot-websocket/src/main/java/com/heibaiying/springboot/websocket/WebSocketConfig.java b/spring-boot/spring-boot-websocket/src/main/java/com/heibaiying/springboot/websocket/WebSocketConfig.java new file mode 100644 index 0000000..4bba5dd --- /dev/null +++ b/spring-boot/spring-boot-websocket/src/main/java/com/heibaiying/springboot/websocket/WebSocketConfig.java @@ -0,0 +1,17 @@ +package com.heibaiying.springboot.websocket; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.socket.server.standard.ServerEndpointExporter; + +/** + * @author : heibaiying + */ +@Configuration +public class WebSocketConfig { + + @Bean + public ServerEndpointExporter serverEndpointExporter() { + return new ServerEndpointExporter(); + } +} diff --git a/spring-boot/spring-boot-websocket/src/main/resources/application.properties b/spring-boot/spring-boot-websocket/src/main/resources/application.properties new file mode 100644 index 0000000..e69de29 diff --git a/spring-boot/spring-boot-websocket/src/main/resources/templates/chat.ftl b/spring-boot/spring-boot-websocket/src/main/resources/templates/chat.ftl new file mode 100644 index 0000000..f633e15 --- /dev/null +++ b/spring-boot/spring-boot-websocket/src/main/resources/templates/chat.ftl @@ -0,0 +1,39 @@ + + + + ${Session["username"]}您好!欢迎进入群聊大厅! + + +
${Session["username"]}您好!欢迎进入群聊大厅!
+ + +
+ +
+ + + diff --git a/spring-boot/spring-boot-websocket/src/main/resources/templates/index.ftl b/spring-boot/spring-boot-websocket/src/main/resources/templates/index.ftl new file mode 100644 index 0000000..71dc9a9 --- /dev/null +++ b/spring-boot/spring-boot-websocket/src/main/resources/templates/index.ftl @@ -0,0 +1,12 @@ + + + + Title + + +
+ + +
+ + diff --git a/spring-boot/spring-boot-websocket/src/test/java/com/heibaiying/springboot/SpringBootWebsocketApplicationTests.java b/spring-boot/spring-boot-websocket/src/test/java/com/heibaiying/springboot/SpringBootWebsocketApplicationTests.java new file mode 100644 index 0000000..56c6b68 --- /dev/null +++ b/spring-boot/spring-boot-websocket/src/test/java/com/heibaiying/springboot/SpringBootWebsocketApplicationTests.java @@ -0,0 +1,17 @@ +package com.heibaiying.springboot; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@SpringBootTest +public class SpringBootWebsocketApplicationTests { + + @Test + public void contextLoads() { + } + +} + diff --git a/spring/spring-websocket-annotation/src/main/java/com/heibaiying/websocket/CustomerHandler.java b/spring/spring-websocket-annotation/src/main/java/com/heibaiying/websocket/CustomerHandler.java index 4dca73a..5e41ab8 100644 --- a/spring/spring-websocket-annotation/src/main/java/com/heibaiying/websocket/CustomerHandler.java +++ b/spring/spring-websocket-annotation/src/main/java/com/heibaiying/websocket/CustomerHandler.java @@ -23,6 +23,7 @@ public class CustomerHandler extends TextWebSocketHandler { @Override public void afterConnectionEstablished(WebSocketSession session) { String username = getNameFromSession(session); + //这个方法是线程不安全的 nameAndSession.putIfAbsent(username, session); }