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 817659bd23 Feature/fix no javax validation dependency&spring 
controller support (#12085)
817659bd23 is described below

commit 817659bd2320810e1096785433a08bd7ef73a72c
Author: suncairong163 <[email protected]>
AuthorDate: Thu Apr 20 15:12:51 2023 +0800

    Feature/fix no javax validation dependency&spring controller support 
(#12085)
    
    * Simplify rest client
    
    * Fix protocol
    
    * fix codec
    
    * tmp disable test
    
    * rest metadata resolver add  interface judge
    
    * add rest metadata  resolve unit test & fix  
AbstractServiceRestMetadataResolver
    
    * org.apache.dubbo.metadata.rest.ParamType null exclude & add default 
accept header
    
    * RestProtocolTest
    
    refer add context path
    unit test
    
    * some fix
    
    * ADD TODO
    
    * RESOLVE HTTP client java.net.SocketException: socket closed
    
    * RESOLVE HTTP client java.net.SocketException: socket closed
    
    * add spring mvc rest protocol unit test
    
    * rest protocol http response code deal
    
    * rest protocol http response message
    
    * fix some review advice
    
    * fix some review advice
    
    * add rest metadata  resolve unit test & fix  
AbstractServiceRestMetadataResolver
    
    * org.apache.dubbo.metadata.rest.ParamType null exclude & add default 
accept header
    
    * RestProtocolTest
    
    refer add context path
    unit test
    
    * some fix
    
    * fix some review advice
    
    * add spring mvc rest protocol unit test
    
    * rest protocol http response code deal
    
    * rest protocol http response message
    
    * URLConnectionRestClient getMessage
    
    * remove unused import
    
    * import fix
    
    * code merge
    
    * remove unused import
    
    * code merge
    
    * code merge
    
    * some fix
    
    * code merge
    
    * code merge
    
    * code merge
    
    * code merge
    
    * Rest http server
    
    * rest protocol do export
    
    * code merge
    
    * rest protocol provider
    
    * rest protocol provider
    
    * rest protocol  response
    
    * rest protocol  netty request
    
    * Fix conflicts
    
    * change  restResult InputStream to bytes protect from  fd leak
    
    * merge code
    
    * Fix import
    
    * Fix import
    
    * Fix uts
    
    * Remove unused code
    
    * Fix logger
    
    * Update okhttp version
    
    * Update okhttp version
    
    * rest protocol  add AnotherUserRestService service ut
    
    * rest protocol  add String & byteArray codec
    
    * Fix version
    
    * Fix uts
    
    * rest protocol  add XMLCodec
    
    * stream release
    
    * ServiceRestMetadata port change int to Integer
    
    * service RestMetadata service map init
    
    * xml codec change  for xxe
    
    * Fix import
    
    * code style
    
    * code style
    
    * Fix shade
    
    * change for rest client recreate & destroy bugs
    
    * remove recreate double check for code merge
    
    * rest client destroy check
    
    * add todo
    
    * consumer merge code
    
    * rest response
    
    * BodyProviderParamParser bytes
    
    * merge upstream 3.2 to 3.2_consumer_proxy_invocation_handler
    
    * merge upstream 3.2 to feature/3.2-rest_protocol_provider
    
    * fix rest provider ut
    
    * path compare
    
    * path compare
    
    * fix provider  rest ut
    
    * fix provider  rest ut
    
    * fix provider  rest ut
    
    * testJaxrsPathPattern ut
    
    * JAXRSServiceRestMetadataResolverTest  ut
    
    * JAXRSServiceRestMetadataResolverTest  ut
    
    * add netty http server
    
    * add netty http server response encode
    
    * rest netty  http server  codec
    
    * rest netty  http server  codec
    
    * some fix  netty http server
    
    * add spring mvc ut  for netty http server
    
    * okhttp_version to 3.14.2 to resolve dependency conflict
    
    * Fix license
    
    * add rest protocol exception mapper
    
    * add TODO ExceptionMapper should be static or instance
    
    * add TODO OKHttpRestClient implements of version >=4.0
    
    * fix licence format
    
    * fix ut
    
    * fix testPathMatcher
    
    * url context path
    
    * format context path from url
    
    * fix illegal logger method invocations:
    
    * remove org.jetbrains.annotations.NotNull
    
    * add Newly created SPI interface  to dubbo-all
    
    * some  format
    
    * merge 3.2
    
    * merge 3.2
    
    * merge 3.2
    
    * Revert "merge 3.2"
    
    This reverts commit ad19804437f22d422578572b8f8fbc8b63b68b09.
    
    * merge upstream 3.2
    
    * change HttpHandler throws   IOException
    
    * deal with  RPCInvocation build error
    
    * request is present
    
    * remove  request facade unused code
    
    * remove   thirdpart web  server code
    
    * remove   thirdpart web  server code
    
    * remove   unused imports
    
    * remove   unused code
    
    * rest protocol directly implement AbstractProtocol
    
    * code style
    
    * change method  modifier
    
    * form body  java bean convert
    
    * change header name  to RestHeaderEnum
    
    * attachment header
    
    * http handler error info
    
    * rest header distinguish from attachment
    
    * some log & double path check
    
    * remove RestMethodMetadata ServiceRestMetadata refer
    
    * reconstruct http handler
    
    * content-type judge
    
    * add RestInvoker
    
    * setTargetServiceUniqueName
    
    * path mapper & http handler
    
    * remove unused code
    
    * netty thread pool
    
    * RestHttpRequestDecoder @ChannelHandler.Sharable
    
    * add rest client ut
    
    * fix metadata resolver test
    
    * fix metadata resolver test
    
    * add some description to code
    
    * add error test
    
    * error message append
    
    * add request address
    
    * request address
    
    * http netty server reconstruct
    
    * http netty server TODO add SslServerTlsHandler
    
    * replace all pair use
    
    * pair fix
    
    * add NumberUtils ut
    
    * add hex number  ut
    
    * add no annotation primitive ut
    
    * remove unsed code
    
    * throw exception
    
    * add ut
    
    * rename constants & remove unused code
    
    * add ut
    
    * add ut
    
    * add ut
    
    * add ut
    
    * remove ut
    
    * add restClient ut
    
    * add restClient ut
    
    * add DataParseUtils ut
    
    * add path matcher ut
    
    * add request template ut
    
    * add request template ut
    
    * fix xml codec
    
    * some fix
    
    * Class to Class<?>
    
    * media type judge fix and add codec ut
    
    * remove print
    
    * add header map param ut
    
    * add header map param null ut
    
    * MediaTypeUtil ut
    
    * add param & header & multimap     ut
    
    * remove unused import
    
    * fix error log format
    
    * test header param
    
    * test header number  param
    
    * add MediaType ut
    
    * add MediaType & metadata resolve ut
    
    * imports
    
    * add netty request facade ut
    
    * some  format
    
    * add ExceptionMapperTest
    
    * add hasExceptionMapper pre judge
    
    * fix exception mapper throws
    
    * change  static ExceptionMapper to instance
    
    * resolve Java_Zulu_jdk/17.0.6-10/x64 param is not throwable , exception
    mapper ut
    
    * fix reflects util some detail
    
    * remove unused code
    
    * add null arg ut
    
    * rest invoker response future complete Exceptionally
    
    * fix http netty server executor
    
    * set rpc context request & response
    
    * add rest token attachment
    
    * add http method ut
    
    * remove unused code
    
    * some fix
    
    * remove keep-alive header
    
    * remove unused code
    
    * close channel
    
    * add response connection:close head
    
    * fix no content-type
    
    * fix no content-type form
    
    * spring @RequestMapping or @RestController
    
    * add exception mapper  type judge
    
    * remove content-length of OKHttpRestClient
     GET request
    
    * remove unused import
    
    * fix ConstraintViolationException not exist error
    
    * fix handleConstraintViolationException
    
    * AbstractServiceRestMetadataResolver support spring controller service
    
    * service type null judge
    
    * spring controller support
    
    * add todo
    
    * ConstraintViolationException dependency pre judge
    
    ---------
    
    Co-authored-by: Albumen Kevin <[email protected]>
    Co-authored-by: suncr <[email protected]>
---
 .../rest/AbstractServiceRestMetadataResolver.java  | 22 ++++---
 .../metadata/rest/api/SpringControllerService.java | 71 ++++++++++++++++++++++
 .../SpringMvcServiceRestMetadataResolverTest.java  |  2 +
 .../rpc/protocol/rest/RpcExceptionMapper.java      | 28 ++++-----
 .../rest/annotation/ParamParserManager.java        |  4 ++
 .../ConstraintViolationExceptionConvert.java}      | 26 +++++---
 6 files changed, 123 insertions(+), 30 deletions(-)

diff --git 
a/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/rest/AbstractServiceRestMetadataResolver.java
 
b/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/rest/AbstractServiceRestMetadataResolver.java
index 206fd1fad7..615911e93c 100644
--- 
a/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/rest/AbstractServiceRestMetadataResolver.java
+++ 
b/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/rest/AbstractServiceRestMetadataResolver.java
@@ -72,15 +72,17 @@ public abstract class AbstractServiceRestMetadataResolver 
implements ServiceRest
 
     @Override
     public final boolean supports(Class<?> serviceType, boolean consumer) {
-        // for consumer
-        if (consumer) {
-            //  it is possible serviceType is impl
-            return supports0(serviceType);
+
+        if (serviceType == null) {
+            return false;
         }
 
+        // for consumer
+        //  it is possible serviceType is impl
         // for provider
         // for xml config bean  && isServiceAnnotationPresent(serviceType)
-        return isImplementedInterface(serviceType) && supports0(serviceType);
+        //  isImplementedInterface(serviceType) SpringController
+        return supports0(serviceType);
     }
 
     protected final boolean isImplementedInterface(Class<?> serviceType) {
@@ -172,9 +174,15 @@ public abstract class AbstractServiceRestMetadataResolver 
implements ServiceRest
         // exclude the public methods declared in java.lang.Object.class
         List<Method> declaredServiceMethods = new 
ArrayList<>(getAllMethods(serviceInterfaceClass, 
excludedDeclaredClass(Object.class)));
 
+        // controller class
+        if (serviceType.equals(serviceInterfaceClass)) {
+            putServiceMethodToMap(serviceMethodsMap, declaredServiceMethods);
+            return unmodifiableMap(serviceMethodsMap);
+        }
+
         // for interface , such as consumer interface
         if (serviceType.isInterface()) {
-            putInterfaceMethodToMap(serviceMethodsMap, declaredServiceMethods);
+            putServiceMethodToMap(serviceMethodsMap, declaredServiceMethods);
             return unmodifiableMap(serviceMethodsMap);
         }
 
@@ -198,7 +206,7 @@ public abstract class AbstractServiceRestMetadataResolver 
implements ServiceRest
         return unmodifiableMap(serviceMethodsMap);
     }
 
-    private void putInterfaceMethodToMap(Map<Method, Method> 
serviceMethodsMap, List<Method> declaredServiceMethods) {
+    private void putServiceMethodToMap(Map<Method, Method> serviceMethodsMap, 
List<Method> declaredServiceMethods) {
         declaredServiceMethods.stream().forEach(method -> {
 
             // filter static private default
diff --git 
a/dubbo-metadata/dubbo-metadata-api/src/test/java/org/apache/dubbo/metadata/rest/api/SpringControllerService.java
 
b/dubbo-metadata/dubbo-metadata-api/src/test/java/org/apache/dubbo/metadata/rest/api/SpringControllerService.java
new file mode 100644
index 0000000000..dfea020905
--- /dev/null
+++ 
b/dubbo-metadata/dubbo-metadata-api/src/test/java/org/apache/dubbo/metadata/rest/api/SpringControllerService.java
@@ -0,0 +1,71 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.dubbo.metadata.rest.api;
+
+import org.apache.dubbo.metadata.rest.User;
+import org.springframework.http.MediaType;
+import org.springframework.util.MultiValueMap;
+import org.springframework.web.bind.annotation.PathVariable;
+import org.springframework.web.bind.annotation.RequestBody;
+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;
+
+@RestController
+public class SpringControllerService {
+    @RequestMapping(value = "/param", method = RequestMethod.GET, consumes = 
MediaType.TEXT_PLAIN_VALUE, produces = MediaType.TEXT_PLAIN_VALUE)
+    public String param(@RequestParam String param) {
+        return param;
+    }
+
+    @RequestMapping(value = "/header", method = RequestMethod.GET, consumes = 
MediaType.TEXT_PLAIN_VALUE, produces = MediaType.TEXT_PLAIN_VALUE)
+    public String header(@RequestHeader String header) {
+        return header;
+    }
+
+    @RequestMapping(value = "/body", method = RequestMethod.POST, consumes = 
MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
+    public User body(@RequestBody User user) {
+        return user;
+    }
+
+    @RequestMapping(value = "/multiValue", method = RequestMethod.POST, 
consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE, produces = 
MediaType.APPLICATION_FORM_URLENCODED_VALUE)
+    public MultiValueMap multiValue(@RequestBody MultiValueMap map) {
+        return map;
+    }
+
+    @RequestMapping(value = "/pathVariable/{a}", method = RequestMethod.POST, 
consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE, produces = 
MediaType.APPLICATION_FORM_URLENCODED_VALUE)
+    public String pathVariable(@PathVariable String a) {
+        return a;
+    }
+
+    @RequestMapping(value = "/noAnnoParam", method = RequestMethod.POST, 
consumes = MediaType.TEXT_PLAIN_VALUE, produces = MediaType.TEXT_PLAIN_VALUE)
+    public String noAnnoParam(String a) {
+        return a;
+    }
+
+    @RequestMapping(value = "/noAnnoNumber", method = RequestMethod.POST, 
consumes = MediaType.ALL_VALUE, produces = MediaType.ALL_VALUE)
+    public int noAnnoNumber(Integer b) {
+        return b;
+    }
+
+    @RequestMapping(value = "/noAnnoPrimitive", method = RequestMethod.POST, 
consumes = MediaType.ALL_VALUE, produces = MediaType.ALL_VALUE)
+    public int noAnnoPrimitive(int c) {
+        return c;
+    }
+}
diff --git 
a/dubbo-metadata/dubbo-metadata-api/src/test/java/org/apache/dubbo/metadata/rest/springmvc/SpringMvcServiceRestMetadataResolverTest.java
 
b/dubbo-metadata/dubbo-metadata-api/src/test/java/org/apache/dubbo/metadata/rest/springmvc/SpringMvcServiceRestMetadataResolverTest.java
index 40f123a952..a79c6e5061 100644
--- 
a/dubbo-metadata/dubbo-metadata-api/src/test/java/org/apache/dubbo/metadata/rest/springmvc/SpringMvcServiceRestMetadataResolverTest.java
+++ 
b/dubbo-metadata/dubbo-metadata-api/src/test/java/org/apache/dubbo/metadata/rest/springmvc/SpringMvcServiceRestMetadataResolverTest.java
@@ -25,6 +25,7 @@ import org.apache.dubbo.metadata.rest.RestMethodMetadata;
 import org.apache.dubbo.metadata.rest.RestService;
 import org.apache.dubbo.metadata.rest.ServiceRestMetadata;
 import org.apache.dubbo.metadata.rest.StandardRestService;
+import org.apache.dubbo.metadata.rest.api.SpringControllerService;
 import org.apache.dubbo.metadata.rest.api.SpringRestService;
 import org.apache.dubbo.metadata.rest.api.SpringRestServiceImpl;
 import org.apache.dubbo.rpc.model.ApplicationModel;
@@ -86,6 +87,7 @@ class SpringMvcServiceRestMetadataResolverTest {
     void testResolves() {
         testResolve(SpringRestService.class);
         testResolve(SpringRestServiceImpl.class);
+        testResolve(SpringControllerService.class);
     }
 
     void testResolve(Class service) {
diff --git 
a/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/RpcExceptionMapper.java
 
b/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/RpcExceptionMapper.java
index a50a2dda5a..fe07516ba4 100644
--- 
a/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/RpcExceptionMapper.java
+++ 
b/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/RpcExceptionMapper.java
@@ -16,30 +16,30 @@
  */
 package org.apache.dubbo.rpc.protocol.rest;
 
+import org.apache.dubbo.common.utils.ClassUtils;
 import org.apache.dubbo.rpc.RpcException;
 import org.apache.dubbo.rpc.protocol.rest.exception.mapper.ExceptionHandler;
+import 
org.apache.dubbo.rpc.protocol.rest.util.ConstraintViolationExceptionConvert;
 
-import javax.validation.ConstraintViolation;
-import javax.validation.ConstraintViolationException;
 
 public class RpcExceptionMapper implements ExceptionHandler<RpcException> {
 
-    protected Object 
handleConstraintViolationException(ConstraintViolationException cve) {
-        ViolationReport report = new ViolationReport();
-        for (ConstraintViolation<?> cv : cve.getConstraintViolations()) {
-            report.addConstraintViolation(new RestConstraintViolation(
-                cv.getPropertyPath().toString(),
-                cv.getMessage(),
-                cv.getInvalidValue() == null ? "null" : 
cv.getInvalidValue().toString()));
-        }
-        return report;
-    }
 
     @Override
     public Object result(RpcException e) {
-        if (e.getCause() instanceof ConstraintViolationException) {
-            return 
handleConstraintViolationException((ConstraintViolationException) e.getCause());
+
+        // javax dependency judge
+        if (violationDependency()) {
+            //  ConstraintViolationException judge
+            if (ConstraintViolationExceptionConvert.needConvert(e)) {
+                return 
ConstraintViolationExceptionConvert.handleConstraintViolationException(e);
+            }
         }
+
         return "Internal server error: " + e.getMessage();
     }
+
+    private boolean violationDependency() {
+        return 
ClassUtils.isPresent("javax.validation.ConstraintViolationException", 
RpcExceptionMapper.class.getClassLoader());
+    }
 }
diff --git 
a/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/annotation/ParamParserManager.java
 
b/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/annotation/ParamParserManager.java
index fb865bf417..5c0dbaa236 100644
--- 
a/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/annotation/ParamParserManager.java
+++ 
b/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/annotation/ParamParserManager.java
@@ -56,6 +56,7 @@ public class ParamParserManager {
                 paramParser.parse(parseContext, args.get(i));
             }
         }
+        // TODO add param require or default & body arg size pre judge
         return parseContext.getArgs().toArray(new Object[0]);
     }
 
@@ -86,5 +87,8 @@ public class ParamParserManager {
             }
         }
 
+        // TODO add param require or default
+
+
     }
 }
diff --git 
a/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/RpcExceptionMapper.java
 
b/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/util/ConstraintViolationExceptionConvert.java
similarity index 64%
copy from 
dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/RpcExceptionMapper.java
copy to 
dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/util/ConstraintViolationExceptionConvert.java
index a50a2dda5a..df0e34f057 100644
--- 
a/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/RpcExceptionMapper.java
+++ 
b/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/util/ConstraintViolationExceptionConvert.java
@@ -14,17 +14,21 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package org.apache.dubbo.rpc.protocol.rest;
+package org.apache.dubbo.rpc.protocol.rest.util;
 
 import org.apache.dubbo.rpc.RpcException;
-import org.apache.dubbo.rpc.protocol.rest.exception.mapper.ExceptionHandler;
+import org.apache.dubbo.rpc.protocol.rest.RestConstraintViolation;
+import org.apache.dubbo.rpc.protocol.rest.ViolationReport;
 
 import javax.validation.ConstraintViolation;
 import javax.validation.ConstraintViolationException;
 
-public class RpcExceptionMapper implements ExceptionHandler<RpcException> {
+public class ConstraintViolationExceptionConvert {
 
-    protected Object 
handleConstraintViolationException(ConstraintViolationException cve) {
+
+
+    public static Object handleConstraintViolationException(RpcException 
rpcException) {
+        ConstraintViolationException cve = (ConstraintViolationException) 
rpcException.getCause();
         ViolationReport report = new ViolationReport();
         for (ConstraintViolation<?> cv : cve.getConstraintViolations()) {
             report.addConstraintViolation(new RestConstraintViolation(
@@ -35,11 +39,15 @@ public class RpcExceptionMapper implements 
ExceptionHandler<RpcException> {
         return report;
     }
 
-    @Override
-    public Object result(RpcException e) {
-        if (e.getCause() instanceof ConstraintViolationException) {
-            return 
handleConstraintViolationException((ConstraintViolationException) e.getCause());
+    public static boolean needConvert(RpcException e) {
+        return isConstraintViolationException(e);
+    }
+
+    private static boolean isConstraintViolationException(RpcException e) {
+        try {
+            return e.getCause() instanceof ConstraintViolationException;
+        } catch (Throwable throwable) {
+            return false;
         }
-        return "Internal server error: " + e.getMessage();
     }
 }

Reply via email to