This is an automated email from the ASF dual-hosted git repository.
albumenj pushed a commit to branch 3.2
in repository https://gitbox.apache.org/repos/asf/dubbo.git
The following commit(s) were added to refs/heads/3.2 by this push:
new 308aea12ec Feature/dubbo rest 3.2 spring annotaion fix (#12043)
308aea12ec is described below
commit 308aea12ec0c1315522d18ececd2dc90e8c51ed5
Author: suncairong163 <[email protected]>
AuthorDate: Mon Apr 10 19:12:01 2023 +0800
Feature/dubbo rest 3.2 spring annotaion fix (#12043)
---
.../SpringMvcServiceRestMetadataResolver.java | 7 +++---
.../SpringMvcServiceRestMetadataResolver.java | 21 +++++++++--------
.../remoting/http/restclient/OKHttpRestClient.java | 18 +++++++++++----
.../rpc/protocol/rest/NettyHttpRestServer.java | 7 ++----
.../rest/exception/mapper/ExceptionMapper.java | 12 ++++++++--
.../rpc/protocol/rest/JaxrsRestProtocolTest.java | 2 +-
.../protocol/rest/SpringMvcRestProtocolTest.java | 27 ++++++++++++++++++++--
.../protocol/rest/mvc/SpringDemoServiceImpl.java | 22 ++++++++++++++++++
.../protocol/rest/mvc/SpringRestDemoService.java | 16 +++++++++++--
9 files changed, 102 insertions(+), 30 deletions(-)
diff --git
a/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/rest/springmvc/SpringMvcServiceRestMetadataResolver.java
b/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/rest/springmvc/SpringMvcServiceRestMetadataResolver.java
index 135696df6a..f7e89540c4 100644
---
a/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/rest/springmvc/SpringMvcServiceRestMetadataResolver.java
+++
b/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/rest/springmvc/SpringMvcServiceRestMetadataResolver.java
@@ -55,13 +55,14 @@ public class SpringMvcServiceRestMetadataResolver extends
AbstractServiceRestMet
@Override
protected boolean supports0(Class<?> serviceType) {
- return isAnnotationPresent(serviceType, CONTROLLER_ANNOTATION_CLASS);
+ // class @Controller or @RequestMapping
+ return isAnnotationPresent(serviceType, CONTROLLER_ANNOTATION_CLASS)
|| isAnnotationPresent(serviceType, REQUEST_MAPPING_ANNOTATION_CLASS);
}
@Override
protected boolean isRestCapableMethod(Method serviceMethod, Class<?>
serviceType, Class<?> serviceInterfaceClass) {
- return isAnnotationPresent(serviceType,
REQUEST_MAPPING_ANNOTATION_CLASS) ||
- isAnnotationPresent(serviceMethod,
REQUEST_MAPPING_ANNOTATION_CLASS);
+ // method only match @RequestMapping
+ return isAnnotationPresent(serviceMethod,
REQUEST_MAPPING_ANNOTATION_CLASS);
}
@Override
diff --git
a/dubbo-metadata/dubbo-metadata-processor/src/main/java/org/apache/dubbo/metadata/annotation/processing/rest/springmvc/SpringMvcServiceRestMetadataResolver.java
b/dubbo-metadata/dubbo-metadata-processor/src/main/java/org/apache/dubbo/metadata/annotation/processing/rest/springmvc/SpringMvcServiceRestMetadataResolver.java
index 713bf74fa0..e33fdb3f1d 100644
---
a/dubbo-metadata/dubbo-metadata-processor/src/main/java/org/apache/dubbo/metadata/annotation/processing/rest/springmvc/SpringMvcServiceRestMetadataResolver.java
+++
b/dubbo-metadata/dubbo-metadata-processor/src/main/java/org/apache/dubbo/metadata/annotation/processing/rest/springmvc/SpringMvcServiceRestMetadataResolver.java
@@ -64,7 +64,8 @@ public class SpringMvcServiceRestMetadataResolver extends
AbstractServiceRestMet
}
public static boolean supports(TypeElement serviceType) {
- return isAnnotationPresent(serviceType,
CONTROLLER_ANNOTATION_CLASS_NAME);
+ // class @Controller or @RequestMapping
+ return isAnnotationPresent(serviceType,
CONTROLLER_ANNOTATION_CLASS_NAME) || isAnnotationPresent(serviceType,
REQUEST_MAPPING_ANNOTATION_CLASS_NAME);
}
@Override
@@ -131,15 +132,15 @@ public class SpringMvcServiceRestMetadataResolver extends
AbstractServiceRestMet
private AnnotationMirror getMappingAnnotation(Element element) {
return computeIfAbsent(valueOf(element), key ->
- filterFirst(getAllAnnotations(element), annotation -> {
- DeclaredType annotationType =
annotation.getAnnotationType();
- // try "@RequestMapping" first
- if
(REQUEST_MAPPING_ANNOTATION_CLASS_NAME.equals(annotationType.toString())) {
- return true;
- }
- // try meta annotation
- return isAnnotationPresent(annotationType.asElement(),
REQUEST_MAPPING_ANNOTATION_CLASS_NAME);
- })
+ filterFirst(getAllAnnotations(element), annotation -> {
+ DeclaredType annotationType = annotation.getAnnotationType();
+ // try "@RequestMapping" first
+ if
(REQUEST_MAPPING_ANNOTATION_CLASS_NAME.equals(annotationType.toString())) {
+ return true;
+ }
+ // try meta annotation
+ return isAnnotationPresent(annotationType.asElement(),
REQUEST_MAPPING_ANNOTATION_CLASS_NAME);
+ })
);
}
diff --git
a/dubbo-remoting/dubbo-remoting-http/src/main/java/org/apache/dubbo/remoting/http/restclient/OKHttpRestClient.java
b/dubbo-remoting/dubbo-remoting-http/src/main/java/org/apache/dubbo/remoting/http/restclient/OKHttpRestClient.java
index 975cc9d98b..461d2bcd62 100644
---
a/dubbo-remoting/dubbo-remoting-http/src/main/java/org/apache/dubbo/remoting/http/restclient/OKHttpRestClient.java
+++
b/dubbo-remoting/dubbo-remoting-http/src/main/java/org/apache/dubbo/remoting/http/restclient/OKHttpRestClient.java
@@ -56,18 +56,26 @@ public class OKHttpRestClient implements RestClient {
Map<String, Collection<String>> allHeaders =
requestTemplate.getAllHeaders();
+ boolean hasBody = false;
+ RequestBody requestBody = null;
+ // GET & HEAD body is forbidden
+ if (HttpMethod.permitsRequestBody(requestTemplate.getHttpMethod())) {
+ requestBody = RequestBody.create(null,
requestTemplate.getSerializedBody());
+ hasBody = true;
+ }
+
// header
for (String headerName : allHeaders.keySet()) {
Collection<String> headerValues = allHeaders.get(headerName);
-
+ if (!hasBody && "Content-Length".equals(headerName)) {
+ continue;
+ }
for (String headerValue : headerValues) {
+
builder.addHeader(headerName, headerValue);
}
}
- RequestBody requestBody = null;
- if (HttpMethod.permitsRequestBody(requestTemplate.getHttpMethod())) {
- requestBody = RequestBody.create(null,
requestTemplate.getSerializedBody());
- }
+
builder.method(requestTemplate.getHttpMethod(), requestBody);
CompletableFuture<RestResult> future = new CompletableFuture<>();
diff --git
a/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/NettyHttpRestServer.java
b/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/NettyHttpRestServer.java
index f757fafced..d65c368ce6 100644
---
a/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/NettyHttpRestServer.java
+++
b/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/NettyHttpRestServer.java
@@ -49,9 +49,6 @@ import static
org.apache.dubbo.common.constants.CommonConstants.IO_THREADS_KEY;
import static org.apache.dubbo.remoting.Constants.BIND_IP_KEY;
import static org.apache.dubbo.remoting.Constants.BIND_PORT_KEY;
import static org.apache.dubbo.remoting.Constants.DEFAULT_IO_THREADS;
-import static org.apache.dubbo.rpc.protocol.rest.Constants.DEFAULT_KEEP_ALIVE;
-import static
org.apache.dubbo.rpc.protocol.rest.Constants.EXCEPTION_MAPPER_KEY;
-import static org.apache.dubbo.rpc.protocol.rest.Constants.KEEP_ALIVE_KEY;
/**
* netty http server
@@ -148,7 +145,7 @@ public class NettyHttpRestServer implements
RestProtocolServer {
*/
protected Map<ChannelOption, Object> getChildChannelOptionMap(URL url) {
Map<ChannelOption, Object> channelOption = new HashMap<>();
- channelOption.put(ChannelOption.SO_KEEPALIVE,
url.getParameter(KEEP_ALIVE_KEY, DEFAULT_KEEP_ALIVE));
+ channelOption.put(ChannelOption.SO_KEEPALIVE,
url.getParameter(Constants.KEEP_ALIVE_KEY, Constants.DEFAULT_KEEP_ALIVE));
return channelOption;
}
@@ -205,7 +202,7 @@ public class NettyHttpRestServer implements
RestProtocolServer {
private void registerExceptionMapper(URL url) {
- for (String clazz :
COMMA_SPLIT_PATTERN.split(url.getParameter(EXCEPTION_MAPPER_KEY,
RpcExceptionMapper.class.getName()))) {
+ for (String clazz :
COMMA_SPLIT_PATTERN.split(url.getParameter(Constants.EXTENSION_KEY,
RpcExceptionMapper.class.getName()))) {
if (!StringUtils.isEmpty(clazz)) {
exceptionMapper.registerMapper(clazz);
}
diff --git
a/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/exception/mapper/ExceptionMapper.java
b/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/exception/mapper/ExceptionMapper.java
index 3ea8048042..cbfc1ad1e5 100644
---
a/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/exception/mapper/ExceptionMapper.java
+++
b/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/exception/mapper/ExceptionMapper.java
@@ -17,6 +17,8 @@
package org.apache.dubbo.rpc.protocol.rest.exception.mapper;
+import org.apache.dubbo.common.logger.ErrorTypeAwareLogger;
+import org.apache.dubbo.common.logger.LoggerFactory;
import org.apache.dubbo.rpc.protocol.rest.util.ReflectUtils;
import java.lang.reflect.Constructor;
@@ -29,6 +31,8 @@ import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
public class ExceptionMapper {
+ private final ErrorTypeAwareLogger logger =
LoggerFactory.getErrorTypeAwareLogger(getClass());
+
// TODO static or instance ? think about influence between difference
url exception
private final Map<Class<?>, ExceptionHandler> exceptionHandlerMap = new
ConcurrentHashMap<>();
@@ -51,7 +55,11 @@ public class ExceptionMapper {
public void registerMapper(Class<?> exceptionHandler) {
+
try {
+ if (!ExceptionHandler.class.isAssignableFrom(exceptionHandler)) {
+ return;
+ }
// resolve Java_Zulu_jdk/17.0.6-10/x64 param is not throwable
List<Method> methods =
ReflectUtils.getMethodByNameList(exceptionHandler, "result");
@@ -100,8 +108,8 @@ public class ExceptionMapper {
public void registerMapper(String exceptionMapper) {
try {
registerMapper(ReflectUtils.findClass(exceptionMapper));
- } catch (ClassNotFoundException e) {
- throw new RuntimeException("dubbo rest protocol exception mapper
register error ", e);
+ } catch (Exception e) {
+ logger.warn("", e.getMessage(), "", "dubbo rest protocol exception
mapper register error ,and current exception mapper is :" + exceptionMapper);
}
}
diff --git
a/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/JaxrsRestProtocolTest.java
b/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/JaxrsRestProtocolTest.java
index 86fcbac48a..deca0a6937 100644
---
a/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/JaxrsRestProtocolTest.java
+++
b/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/JaxrsRestProtocolTest.java
@@ -330,7 +330,7 @@ class JaxrsRestProtocolTest {
URL url = this.registerProvider(exportUrl, server, DemoService.class);
- URL exceptionUrl = url.addParameter(EXCEPTION_MAPPER_KEY,
TestExceptionMapper.class.getName());
+ URL exceptionUrl = url.addParameter(EXTENSION_KEY,
TestExceptionMapper.class.getName());
protocol.export(proxy.getInvoker(server, DemoService.class,
exceptionUrl));
diff --git
a/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/SpringMvcRestProtocolTest.java
b/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/SpringMvcRestProtocolTest.java
index a5f4cb0c6d..0cbd1deed1 100644
---
a/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/SpringMvcRestProtocolTest.java
+++
b/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/SpringMvcRestProtocolTest.java
@@ -50,7 +50,6 @@ import java.util.Arrays;
import java.util.Map;
import static org.apache.dubbo.remoting.Constants.SERVER_KEY;
-import static
org.apache.dubbo.rpc.protocol.rest.Constants.EXCEPTION_MAPPER_KEY;
import static org.apache.dubbo.rpc.protocol.rest.Constants.EXTENSION_KEY;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
@@ -90,7 +89,7 @@ public class SpringMvcRestProtocolTest {
public Exporter<SpringRestDemoService> getExceptionHandlerExport(URL url,
SpringRestDemoService server) {
url = url.addParameter(SERVER_KEY, Constants.NETTY_HTTP);
- url = url.addParameter(EXCEPTION_MAPPER_KEY,
TestExceptionMapper.class.getName());
+ url = url.addParameter(EXTENSION_KEY,
TestExceptionMapper.class.getName());
return protocol.export(proxy.getInvoker(server, getServerClass(),
url));
}
@@ -365,6 +364,30 @@ public class SpringMvcRestProtocolTest {
exporter.unexport();
}
+ @Test
+ void testPrimitive() {
+ SpringRestDemoService server = getServerImpl();
+
+ URL nettyUrl = this.registerProvider(exportUrl, server,
SpringRestDemoService.class);
+
+
+ Exporter<SpringRestDemoService> exporter = getExport(nettyUrl, server);
+
+ SpringRestDemoService demoService =
this.proxy.getProxy(protocol.refer(SpringRestDemoService.class, nettyUrl));
+
+ Integer result = demoService.primitiveInt(1, 2);
+ Long resultLong = demoService.primitiveLong(1, 2l);
+ long resultByte = demoService.primitiveByte((byte) 1, 2l);
+ long resultShort = demoService.primitiveShort((short) 1, 2l, 1);
+
+ assertThat(result, is(3));
+ assertThat(resultShort, is(3l));
+ assertThat(resultLong, is(3l));
+ assertThat(resultByte, is(3l));
+
+ exporter.unexport();
+ }
+
public static class TestExceptionMapper implements
ExceptionHandler<RuntimeException> {
@Override
diff --git
a/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/mvc/SpringDemoServiceImpl.java
b/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/mvc/SpringDemoServiceImpl.java
index f06897b076..38a26a6caf 100644
---
a/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/mvc/SpringDemoServiceImpl.java
+++
b/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/mvc/SpringDemoServiceImpl.java
@@ -78,4 +78,26 @@ public class SpringDemoServiceImpl implements
SpringRestDemoService {
}
+ @Override
+ public int primitiveInt(int a, int b) {
+ return a + b;
+ }
+
+ @Override
+ public long primitiveLong(long a, Long b) {
+ return a + b;
+ }
+
+ @Override
+ public long primitiveByte(byte a, Long b) {
+ return a + b;
+ }
+
+ @Override
+ public long primitiveShort(short a, Long b, int c) {
+ return a + b;
+ }
+
+
+
}
diff --git
a/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/mvc/SpringRestDemoService.java
b/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/mvc/SpringRestDemoService.java
index 5aca5f06bd..adb461bf4a 100644
---
a/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/mvc/SpringRestDemoService.java
+++
b/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/mvc/SpringRestDemoService.java
@@ -25,11 +25,10 @@ import
org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
-import org.springframework.web.bind.annotation.RestController;
import java.util.List;
-@RestController("/demoService")
+@RequestMapping("/demoService")
public interface SpringRestDemoService {
@RequestMapping(value = "/hello", method = RequestMethod.GET)
Integer hello(@RequestParam Integer a, @RequestParam Integer b);
@@ -53,4 +52,17 @@ public interface SpringRestDemoService {
@RequestMapping(value = "/testHeaderInt", method = RequestMethod.GET,
consumes = MediaType.TEXT_PLAIN_VALUE)
String testHeaderInt(@RequestHeader int header);
+
+ @RequestMapping(method = RequestMethod.GET, value = "/primitive")
+ int primitiveInt(@RequestParam("a") int a, @RequestParam("b") int b);
+
+ @RequestMapping(method = RequestMethod.GET, value = "/primitiveLong")
+ long primitiveLong(@RequestParam("a") long a, @RequestParam("b") Long b);
+
+ @RequestMapping(method = RequestMethod.GET, value = "/primitiveByte")
+ long primitiveByte(@RequestParam("a") byte a, @RequestParam("b") Long b);
+
+
+ @RequestMapping(method = RequestMethod.POST, value = "/primitiveShort")
+ long primitiveShort(@RequestParam("a") short a, @RequestParam("b") Long b,
@RequestBody int c);
}