Skip to content

Commit

Permalink
新增参数入参的注解的实现
Browse files Browse the repository at this point in the history
  • Loading branch information
185594-5-27 committed Jul 17, 2020
1 parent 2e86a52 commit 04e1124
Show file tree
Hide file tree
Showing 14 changed files with 105 additions and 26 deletions.
17 changes: 14 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ websocket-spring-boot-starter [![License](http://img.shields.io/:license-apache-

### 要求
- jdk版本为1.8或1.8+
- spring-boot版本为2.3.1.RELEASE
- spring-boot版本为2.0.1.RELEASE+
- netty版本为4.1.42.Final

### example例子
Expand All @@ -19,7 +19,7 @@ websocket-spring-boot-starter [![License](http://img.shields.io/:license-apache-
<dependency>
<groupId>com.github.lazyboyl</groupId>
<artifactId>websocket-spring-boot-starter</artifactId>
<version>0.0.1-SNAPSHOT</version>
<version>1.0.1.RELEASE</version>
</dependency>
```

Expand Down Expand Up @@ -47,6 +47,12 @@ public class VueDemoApplication {
@WebSocketController
@WebSocketRequestMapping("/user/")
public class UserController {

/**
* 这个userService就是我们平时在spring里面写的service
*/
@Autowired
private UserService userService;

/**
* 功能描述: 模拟字段请求的方式来实现根据用户ID来获取用户数据
Expand Down Expand Up @@ -225,4 +231,9 @@ websocket:
thread:
boss: 12
work: 12
```
```
---
### 版本更新
#### 1.0.1.RELEASE
- `@WebSocketController`注解的类支持构造方法的方式注入的支持。
- 新增`@WebSocketRequestParam`支持入参的自定义。
2 changes: 1 addition & 1 deletion vue-example/netty-websocket-vue-back/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<dependency>
<groupId>com.github.lazyboyl</groupId>
<artifactId>websocket-spring-boot-starter</artifactId>
<version>0.0.3-SNAPSHOT</version>
<version>1.0.1.RELEASE</version>
</dependency>

<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.github.lazyboyl.websocket.annotation.WebSocketController;
import com.github.lazyboyl.websocket.annotation.WebSocketRequestMapping;
import com.github.lazyboyl.websocket.annotation.WebSocketRequestParam;
import com.vue.demo.vue.entity.OrgVo;
import com.vue.demo.vue.entity.UserVo;
import com.vue.demo.vue.service.UserService;
Expand All @@ -22,6 +23,17 @@ public class UserController {
@Autowired
private UserService userService;

/**
* 功能描述: 模拟字段请求且使用别名的方式来实现根据用户ID来获取用户数据
*
* @param userId 用户的流水ID
* @return
*/
@WebSocketRequestMapping("getUserVoByUserIdRename")
public UserVo getUserVoByUserIdRename(@WebSocketRequestParam(name = "uId") String userId) {
return userService.getUserVoByUserId(userId);
}

/**
* 功能描述: 模拟实体请求的方式来实现根据用户ID来获取用户数据
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<button @click="getUserVoByUserOrgVo">getUserVoByUserOrgVo</button>
<button @click="getUserVoByUserList">getUserVoByUserList</button>
<button @click="getOrgVo">getOrgVo</button>
<button @click="getUserVoByUserIdRename">getUserVoByUserIdRename</button>
</div>
</template>

Expand All @@ -23,6 +24,10 @@ export default {
this.initWebSocket()
},
methods: {
getUserVoByUserIdRename () {
let actions = {'url': '/user/getUserVoByUserIdRename/', 'params': {'uId': 'abc'}}
this.websocketsend(JSON.stringify(actions))
},
getOrgVo () {
let actions = {'url': '/org/getOrgVo/', 'params': {'orgId': 'sada11d', 'orgName': '林泽锋'}}
this.websocketsend(JSON.stringify(actions))
Expand Down
10 changes: 2 additions & 8 deletions websocket-spring-boot-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,20 +56,14 @@
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.3.1.RELEASE</version>
<version>2.0.1.RELEASE</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>2.3.1.RELEASE</version>
<version>2.0.1.RELEASE</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.github.lazyboyl.websocket.annotation;

import java.lang.annotation.*;

/**
* 类描述:请求参数的别名的在定义的注解
*
* @author linzef
* @since 2020-07-17
*/
@Target({ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface WebSocketRequestParam {

String name() default "";

}
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,19 @@ public class NettyMethodDefinition {
*/
private String beanName;

/**
* WebSocketRequestParam定义的别名
*/
private String [] requestParamName;

public String[] getRequestParamName() {
return requestParamName;
}

public void setRequestParamName(String[] requestParamName) {
this.requestParamName = requestParamName;
}

public Parameter[] getParameters() {
return parameters;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.lazyboyl.websocket.factory;

import com.github.lazyboyl.websocket.annotation.WebSocketRequestMapping;
import com.github.lazyboyl.websocket.annotation.WebSocketRequestParam;
import com.github.lazyboyl.websocket.beans.NettyBeanDefinition;
import com.github.lazyboyl.websocket.beans.NettyMethodDefinition;
import org.springframework.core.env.Environment;
Expand Down Expand Up @@ -95,6 +96,21 @@ protected void registerNettyMethodDefinition(Class c, NettyBeanDefinition nettyB
nettyMethodDefinition.setMethodName(m.getName());
nettyMethodDefinition.setBeanName(c.getName());
nettyMethodDefinition.setParameters(m.getParameters());
Annotation[][] parameterAnnotations = m.getParameterAnnotations();
String[] requestParamName = new String[parameterAnnotations.length];
for (int i = 0; i < parameterAnnotations.length; i++) {
if (parameterAnnotations[i].length == 0) {
requestParamName[i] = "";
} else {
for (int j = 0; j < parameterAnnotations[i].length; j++) {
if(parameterAnnotations[i][j] instanceof WebSocketRequestParam){
WebSocketRequestParam wsrp = (WebSocketRequestParam) parameterAnnotations[i][j];
requestParamName[i] = wsrp.name();
}
}
}
}
nettyMethodDefinition.setRequestParamName(requestParamName);
methodMap.put(c.getName() + "." + m.getName(), nettyMethodDefinition);
WebSocketRequestMapping a = m.getAnnotation(WebSocketRequestMapping.class);
if (a == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ protected void invokeMethod(ChannelHandlerContext ctx, NettyMethodDefinition net
Parameter[] ps = nettyMethodDefinition.getParameters();
Object[] obj = new Object[ps.length];
Class[] parameterTypesClass = nettyMethodDefinition.getParameterTypesClass();
String [] requestParamName = nettyMethodDefinition.getRequestParamName();
for (int i = 0; i < ps.length; i++) {
Parameter p = ps[i];
if (isMyClass(parameterTypesClass[i])) {
Expand All @@ -219,7 +220,11 @@ protected void invokeMethod(ChannelHandlerContext ctx, NettyMethodDefinition net
} else if ("java.util.".indexOf(parameterTypesClass[i].getName()) != -1) {
SocketUtil.writeAndFlush(ctx.channel(), new SocketResponse(HttpResponseStatus.BAD_REQUEST.code(), "java.util系列暂时只支持map和list,其他类型暂不支持。"));
} else {
obj[i] = paramMap.get(p.getName());
if("".equals(requestParamName[i])){
obj[i] = paramMap.get(p.getName());
} else {
obj[i] = paramMap.get(requestParamName[i]);
}
}
} else {
if (parameterTypesClass[i].getName().equals(HttpHeaders.class.getName())) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.lazyboyl.websocket;

import org.junit.jupiter.api.Test;

import org.junit.Test;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
Expand Down
12 changes: 3 additions & 9 deletions websocket-spring-boot-starter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,26 +42,20 @@
<dependency>
<groupId>com.github.lazyboyl</groupId>
<artifactId>websocket-spring-boot-core</artifactId>
<version>1.0.0.RELEASE</version>
<version>1.0.1.RELEASE</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.3.1.RELEASE</version>
<version>2.0.1.RELEASE</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>2.3.1.RELEASE</version>
<version>2.0.1.RELEASE</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.lazyboyl.websocket;

import org.junit.jupiter.api.Test;

import org.junit.Test;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.github.lazyboyl.websocket.annotation.WebSocketController;
import com.github.lazyboyl.websocket.annotation.WebSocketRequestMapping;
import com.github.lazyboyl.websocket.annotation.WebSocketRequestParam;
import com.wesocket.demo.entity.OrgVo;
import com.wesocket.demo.service.OrgService;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -18,7 +19,7 @@ public class DemoController {
private Integer port;

@Autowired
public DemoController(OrgService orgService,@Value("${websocket.port}") Integer port) {
public DemoController(OrgService orgService, @Value("${websocket.port}") Integer port) {
this.orgService = orgService;
this.port = port;
}
Expand All @@ -29,4 +30,12 @@ public OrgVo getOrgVo111(String orgId, String orgName) {
return orgService.getOrgByOrgId(orgId);
}


@WebSocketRequestMapping("getOrgVo2222")
public OrgVo getOrgVo2222(@WebSocketRequestParam(name = "oId") String orgId, @WebSocketRequestParam(name = "oName") String orgName) {
System.out.println("获取到的请求数据是:" + orgId + "和" + orgName + ",端口是" + port);
return orgService.getOrgByOrgId(orgId);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public int level() {

@Override
public Boolean authentication(ChannelHandlerContext ctx, SocketRequest socketRequest) {
if(socketRequest.getUrl().indexOf("getOrgVo111")!=-1){
if(socketRequest.getUrl().indexOf("getOrgVo111")!=-1 || socketRequest.getUrl().indexOf("getOrgVo2222")!=-1){
return true;
}
Boolean isPass = authService.authUrl(socketRequest.getUrl());
Expand Down

0 comments on commit 04e1124

Please sign in to comment.