[ 
https://issues.apache.org/jira/browse/SCB-989?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16667152#comment-16667152
 ] 

ASF GitHub Bot commented on SCB-989:
------------------------------------

liubao68 closed pull request #972: [SCB-989]scan RestController to to make 
springmvc controller publish as ServiceComb Rest easier
URL: https://github.com/apache/incubator-servicecomb-java-chassis/pull/972
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git 
a/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/RestConst.java
 
b/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/RestConst.java
index 57b4029ae..afdc96bff 100644
--- 
a/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/RestConst.java
+++ 
b/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/RestConst.java
@@ -70,4 +70,6 @@ private RestConst() {
   // the size threshold after which files will be written to disk
   // only available for servlet rest transport
   public static final String UPLOAD_FILE_SIZE_THRESHOLD = 
"servicecomb.uploads.fileSizeThreshold";
+
+  public static final String PROVIDER_SCAN_REST_CONTROLLER = 
"servicecomb.provider.rest.scanRestController";
 }
diff --git 
a/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/ConsumerMain.java
 
b/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/ConsumerMain.java
index b534c5948..61db4463f 100644
--- 
a/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/ConsumerMain.java
+++ 
b/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/ConsumerMain.java
@@ -32,6 +32,7 @@
 import org.apache.servicecomb.it.testcase.TestParamCodec;
 import org.apache.servicecomb.it.testcase.TestParamCodecEdge;
 import org.apache.servicecomb.it.testcase.TestRequestBodySpringMvcSchema;
+import org.apache.servicecomb.it.testcase.TestRestController;
 import org.apache.servicecomb.it.testcase.TestRestServerConfig;
 import org.apache.servicecomb.it.testcase.TestRestServerConfigEdge;
 import org.apache.servicecomb.it.testcase.TestTrace;
@@ -132,6 +133,7 @@ private static void runShareTestCases() throws Throwable {
 
     ITJUnitUtils.run(TestRequestBodySpringMvcSchema.class);
     ITJUnitUtils.run(TestDefaultJsonValueJaxrsSchema.class);
+    ITJUnitUtils.run(TestRestController.class);
   }
 
   private static void testStandalone() throws Throwable {
diff --git 
a/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/extend/engine/ITSCBRestTemplate.java
 
b/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/extend/engine/ITSCBRestTemplate.java
index 8446b9324..b1ed1f95e 100644
--- 
a/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/extend/engine/ITSCBRestTemplate.java
+++ 
b/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/extend/engine/ITSCBRestTemplate.java
@@ -29,6 +29,8 @@
 
   private String schemaId;
 
+  private String basePath;
+
   public ITSCBRestTemplate(String schemaId) {
     this.schemaId = schemaId;
   }
@@ -40,7 +42,8 @@ public ITSCBRestTemplate init() {
             DefinitionConst.VERSION_RULE_ALL);
     MicroserviceVersionMeta microserviceVersionMeta = 
microserviceVersionRule.getLatestMicroserviceVersion();
     SchemaMeta schemaMeta = 
microserviceVersionMeta.getMicroserviceMeta().ensureFindSchemaMeta(schemaId);
-    urlPrefix = String.format("cse://%s/%s", producerName, 
schemaMeta.getSwagger().getBasePath());
+    basePath = schemaMeta.getSwagger().getBasePath();
+    urlPrefix = String.format("cse://%s%s", producerName, basePath);
 
     setUriTemplateHandler(new ITUriTemplateHandler(urlPrefix));
     setRequestFactory(new ITClientHttpRequestFactory());
@@ -48,6 +51,10 @@ public ITSCBRestTemplate init() {
     return this;
   }
 
+  public String getBasePath() {
+    return basePath;
+  }
+
   public String getUrlPrefix() {
     return urlPrefix;
   }
diff --git 
a/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/testcase/TestRestController.java
 
b/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/testcase/TestRestController.java
new file mode 100644
index 000000000..722a43913
--- /dev/null
+++ 
b/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/testcase/TestRestController.java
@@ -0,0 +1,68 @@
+/*
+ * 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.servicecomb.it.testcase;
+
+import org.apache.servicecomb.it.extend.engine.ITSCBRestTemplate;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class TestRestController {
+  private static ITSCBRestTemplate restControllerSchemaClient = new 
ITSCBRestTemplate
+      ("org.apache.servicecomb.it.schema.RestControllerSchema");
+
+  private static ITSCBRestTemplate restControllerEmptyMappingSchemaClient = 
new ITSCBRestTemplate
+      ("org.apache.servicecomb.it.schema.RestControllerEmptyMappingSchema");
+
+  private static ITSCBRestTemplate 
restControllerWithRequestMappingSchemaClient = new ITSCBRestTemplate
+      
("org.apache.servicecomb.it.schema.RestControllerWithRequestMappingSchema");
+
+  private static ITSCBRestTemplate restControllerWithRestSchemaSchemaClient = 
new ITSCBRestTemplate
+      ("RestControllerWithRestSchemaSchema");
+
+  @Test
+  public void restControllerSchemaClient() {
+    Assert.assertEquals("/", restControllerSchemaClient.getBasePath());
+    int result = 
restControllerSchemaClient.getForObject("/restControllerSchemaQuery?input=2", 
int.class);
+    Assert.assertEquals(2, result);
+  }
+
+  @Test
+  public void restControllerEmptyMappingSchemaClient() {
+    // empty path default to class name(@RequestMapping(path = "")). Shall we 
change this behavior in future?
+    Assert.assertEquals("/RestControllerEmptyMappingSchema", 
restControllerEmptyMappingSchemaClient.getBasePath());
+    int result = restControllerEmptyMappingSchemaClient
+        .getForObject("/restControllerEmptyMappingSchemaQuery?input=2", 
int.class);
+    Assert.assertEquals(2, result);
+  }
+
+  @Test
+  public void restControllerWithRequestMappingSchemaClient() {
+    Assert.assertEquals("/restControllerWithRequest", 
restControllerWithRequestMappingSchemaClient.getBasePath());
+    int result = restControllerWithRequestMappingSchemaClient
+        .getForObject("/restControllerWithRequestMappingSchemaQuery?input=2", 
int.class);
+    Assert.assertEquals(2, result);
+  }
+
+  @Test
+  public void restControllerWithRestSchemaSchemaClient() {
+    Assert.assertEquals("/restControllerWithRestSchemaSchema", 
restControllerWithRestSchemaSchemaClient.getBasePath());
+    int result = restControllerWithRestSchemaSchemaClient
+        .getForObject("/restControllerWithRestSchemaSchemaQuery?input=2", 
int.class);
+    Assert.assertEquals(2, result);
+  }
+}
diff --git 
a/integration-tests/it-producer/src/main/java/org/apache/servicecomb/it/schema/RestControllerEmptyMappingSchema.java
 
b/integration-tests/it-producer/src/main/java/org/apache/servicecomb/it/schema/RestControllerEmptyMappingSchema.java
new file mode 100644
index 000000000..ff0cdb5b8
--- /dev/null
+++ 
b/integration-tests/it-producer/src/main/java/org/apache/servicecomb/it/schema/RestControllerEmptyMappingSchema.java
@@ -0,0 +1,31 @@
+/*
+ * 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.servicecomb.it.schema;
+
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.bind.annotation.RestController;
+
+@RestController
+@RequestMapping(path = "")
+public class RestControllerEmptyMappingSchema {
+  @GetMapping(path = "restControllerEmptyMappingSchemaQuery")
+  public int intQuery(@RequestParam(name = "input", required = false, 
defaultValue = "13") int input) {
+    return input;
+  }
+}
diff --git 
a/integration-tests/it-producer/src/main/java/org/apache/servicecomb/it/schema/RestControllerSchema.java
 
b/integration-tests/it-producer/src/main/java/org/apache/servicecomb/it/schema/RestControllerSchema.java
new file mode 100644
index 000000000..08c6b9861
--- /dev/null
+++ 
b/integration-tests/it-producer/src/main/java/org/apache/servicecomb/it/schema/RestControllerSchema.java
@@ -0,0 +1,29 @@
+/*
+ * 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.servicecomb.it.schema;
+
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.bind.annotation.RestController;
+
+@RestController
+public class RestControllerSchema {
+  @GetMapping(path = "restControllerSchemaQuery")
+  public int intQuery(@RequestParam(name = "input", required = false, 
defaultValue = "13") int input) {
+    return input;
+  }
+}
diff --git 
a/integration-tests/it-producer/src/main/java/org/apache/servicecomb/it/schema/RestControllerWithRequestMappingSchema.java
 
b/integration-tests/it-producer/src/main/java/org/apache/servicecomb/it/schema/RestControllerWithRequestMappingSchema.java
new file mode 100644
index 000000000..c0acc9033
--- /dev/null
+++ 
b/integration-tests/it-producer/src/main/java/org/apache/servicecomb/it/schema/RestControllerWithRequestMappingSchema.java
@@ -0,0 +1,31 @@
+/*
+ * 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.servicecomb.it.schema;
+
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.bind.annotation.RestController;
+
+@RestController
+@RequestMapping(path = "/restControllerWithRequest")
+public class RestControllerWithRequestMappingSchema {
+  @GetMapping(path = "restControllerWithRequestMappingSchemaQuery")
+  public int intQuery(@RequestParam(name = "input", required = false, 
defaultValue = "13") int input) {
+    return input;
+  }
+}
diff --git 
a/integration-tests/it-producer/src/main/java/org/apache/servicecomb/it/schema/RestControllerWithRestSchemaSchema.java
 
b/integration-tests/it-producer/src/main/java/org/apache/servicecomb/it/schema/RestControllerWithRestSchemaSchema.java
new file mode 100644
index 000000000..9795aa98d
--- /dev/null
+++ 
b/integration-tests/it-producer/src/main/java/org/apache/servicecomb/it/schema/RestControllerWithRestSchemaSchema.java
@@ -0,0 +1,33 @@
+/*
+ * 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.servicecomb.it.schema;
+
+import org.apache.servicecomb.provider.rest.common.RestSchema;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.bind.annotation.RestController;
+
+@RestController
+@RestSchema(schemaId = "RestControllerWithRestSchemaSchema")
+@RequestMapping(path = "/restControllerWithRestSchemaSchema")
+public class RestControllerWithRestSchemaSchema {
+  @GetMapping(path = "restControllerWithRestSchemaSchemaQuery")
+  public int intQuery(@RequestParam(name = "input", required = false, 
defaultValue = "13") int input) {
+    return input;
+  }
+}
diff --git 
a/providers/provider-rest-common/src/main/java/org/apache/servicecomb/provider/rest/common/RestProducers.java
 
b/providers/provider-rest-common/src/main/java/org/apache/servicecomb/provider/rest/common/RestProducers.java
index 8fac2e201..4b3cedd0b 100644
--- 
a/providers/provider-rest-common/src/main/java/org/apache/servicecomb/provider/rest/common/RestProducers.java
+++ 
b/providers/provider-rest-common/src/main/java/org/apache/servicecomb/provider/rest/common/RestProducers.java
@@ -19,16 +19,23 @@
 import java.util.ArrayList;
 import java.util.List;
 
+import org.apache.servicecomb.common.rest.RestConst;
 import org.apache.servicecomb.core.provider.producer.ProducerMeta;
 import org.apache.servicecomb.foundation.common.utils.BeanUtils;
 import org.springframework.beans.BeansException;
 import org.springframework.beans.factory.config.BeanPostProcessor;
 import org.springframework.stereotype.Component;
+import org.springframework.web.bind.annotation.RestController;
+
+import com.netflix.config.DynamicPropertyFactory;
 
 @Component
 public class RestProducers implements BeanPostProcessor {
   private List<ProducerMeta> producerMetaList = new ArrayList<>();
 
+  private boolean scanRestController = DynamicPropertyFactory.getInstance()
+      .getBooleanProperty(RestConst.PROVIDER_SCAN_REST_CONTROLLER, true).get();
+
   public List<ProducerMeta> getProducerMetaList() {
     return producerMetaList;
   }
@@ -49,15 +56,24 @@ protected void processProvider(String beanName, Object 
bean) {
     // aop后,新的实例的父类可能是原class,也可能只是个proxy,父类不是原class
     // 所以,需要先取出原class,再取标注
     Class<?> beanCls = BeanUtils.getImplClassFromBean(bean);
-    if(beanCls == null) {
-       return;
+    if (beanCls == null) {
+      return;
     }
     RestSchema restSchema = beanCls.getAnnotation(RestSchema.class);
+    ProducerMeta producerMeta;
     if (restSchema == null) {
-      return;
+      if (!scanRestController) {
+        return;
+      }
+      RestController controller = beanCls.getAnnotation(RestController.class);
+      if (controller == null) {
+        return;
+      }
+      producerMeta = new ProducerMeta(beanCls.getName(), bean, beanCls);
+    } else {
+      producerMeta = new ProducerMeta(restSchema.schemaId(), bean, beanCls);
     }
 
-    ProducerMeta producerMeta = new ProducerMeta(restSchema.schemaId(), bean, 
beanCls);
     producerMetaList.add(producerMeta);
   }
 }
diff --git 
a/swagger/swagger-generator/generator-springmvc/src/main/java/org/apache/servicecomb/swagger/generator/springmvc/SpringmvcSwaggerGeneratorContext.java
 
b/swagger/swagger-generator/generator-springmvc/src/main/java/org/apache/servicecomb/swagger/generator/springmvc/SpringmvcSwaggerGeneratorContext.java
index e72978719..6e14a8779 100644
--- 
a/swagger/swagger-generator/generator-springmvc/src/main/java/org/apache/servicecomb/swagger/generator/springmvc/SpringmvcSwaggerGeneratorContext.java
+++ 
b/swagger/swagger-generator/generator-springmvc/src/main/java/org/apache/servicecomb/swagger/generator/springmvc/SpringmvcSwaggerGeneratorContext.java
@@ -35,6 +35,7 @@
 import 
org.apache.servicecomb.swagger.generator.springmvc.processor.annotation.RequestMappingMethodAnnotationProcessor;
 import 
org.apache.servicecomb.swagger.generator.springmvc.processor.annotation.RequestParamAnnotationProcessor;
 import 
org.apache.servicecomb.swagger.generator.springmvc.processor.annotation.RequestPartAnnotationProcessor;
+import 
org.apache.servicecomb.swagger.generator.springmvc.processor.annotation.RestControllerClassAnnotationProcessor;
 import 
org.apache.servicecomb.swagger.generator.springmvc.processor.parameter.SpringmvcDefaultParameterProcessor;
 import org.springframework.web.bind.annotation.CookieValue;
 import org.springframework.web.bind.annotation.DeleteMapping;
@@ -49,6 +50,7 @@
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestParam;
 import org.springframework.web.bind.annotation.RequestPart;
+import org.springframework.web.bind.annotation.RestController;
 
 public class SpringmvcSwaggerGeneratorContext extends 
RestSwaggerGeneratorContext {
   private static final int ORDER = 1000;
@@ -60,7 +62,7 @@ public int getOrder() {
 
   @Override
   public boolean canProcess(Class<?> cls) {
-    return ClassUtils.hasAnnotation(cls, RequestMapping.class);
+    return ClassUtils.hasAnnotation(cls, RequestMapping.class) || 
ClassUtils.hasAnnotation(cls, RestController.class);
   }
 
   @Override
@@ -78,6 +80,7 @@ protected void initClassAnnotationMgr() {
     super.initClassAnnotationMgr();
 
     classAnnotationMgr.register(RequestMapping.class, new 
RequestMappingClassAnnotationProcessor());
+    classAnnotationMgr.register(RestController.class, new 
RestControllerClassAnnotationProcessor());
   }
 
   @Override
diff --git 
a/swagger/swagger-generator/generator-springmvc/src/main/java/org/apache/servicecomb/swagger/generator/springmvc/processor/annotation/RestControllerClassAnnotationProcessor.java
 
b/swagger/swagger-generator/generator-springmvc/src/main/java/org/apache/servicecomb/swagger/generator/springmvc/processor/annotation/RestControllerClassAnnotationProcessor.java
new file mode 100644
index 000000000..01819d6ca
--- /dev/null
+++ 
b/swagger/swagger-generator/generator-springmvc/src/main/java/org/apache/servicecomb/swagger/generator/springmvc/processor/annotation/RestControllerClassAnnotationProcessor.java
@@ -0,0 +1,35 @@
+/*
+ * 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.servicecomb.swagger.generator.springmvc.processor.annotation;
+
+import org.apache.commons.lang3.StringUtils;
+import org.apache.servicecomb.swagger.generator.core.ClassAnnotationProcessor;
+import org.apache.servicecomb.swagger.generator.core.SwaggerGenerator;
+
+import io.swagger.models.Swagger;
+
+public class RestControllerClassAnnotationProcessor implements 
ClassAnnotationProcessor {
+
+  @Override
+  public void process(Object annotation, SwaggerGenerator swaggerGenerator) {
+    Swagger swagger = swaggerGenerator.getSwagger();
+    if (StringUtils.isEmpty(swagger.getBasePath())) {
+      swagger.setBasePath("/");
+    }
+  }
+}


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


> scan RestController to to make springmvc controller publish as ServiceComb 
> Rest easier
> --------------------------------------------------------------------------------------
>
>                 Key: SCB-989
>                 URL: https://issues.apache.org/jira/browse/SCB-989
>             Project: Apache ServiceComb
>          Issue Type: Improvement
>            Reporter: liubao
>            Assignee: liubao
>            Priority: Major
>




--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to