diff --git a/integration-tests/discovery-servicecomb-tests/order-consumer/src/main/java/com/huaweicloud/sample/ScopeRequestController.java b/integration-tests/discovery-servicecomb-tests/order-consumer/src/main/java/com/huaweicloud/sample/ScopeRequestController.java new file mode 100644 index 000000000..1f6ca7466 --- /dev/null +++ b/integration-tests/discovery-servicecomb-tests/order-consumer/src/main/java/com/huaweicloud/sample/ScopeRequestController.java @@ -0,0 +1,31 @@ +/* + + * Copyright (C) 2020-2024 Huawei Technologies Co., Ltd. All rights reserved. + + * Licensed 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 com.huaweicloud.sample; + +import org.springframework.context.annotation.Scope; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@Scope("request") +public class ScopeRequestController { + @GetMapping("testScopeRequest") + public String testScopeRequest() { + return "Hello test scope request"; + } +} diff --git a/spring-cloud-huawei-swagger/src/main/java/com/huaweicloud/swagger/ServiceCombSwaggerHandlerImpl.java b/spring-cloud-huawei-swagger/src/main/java/com/huaweicloud/swagger/ServiceCombSwaggerHandlerImpl.java index d0f7dce5f..ce3f89ce0 100644 --- a/spring-cloud-huawei-swagger/src/main/java/com/huaweicloud/swagger/ServiceCombSwaggerHandlerImpl.java +++ b/spring-cloud-huawei-swagger/src/main/java/com/huaweicloud/swagger/ServiceCombSwaggerHandlerImpl.java @@ -16,6 +16,7 @@ */ package com.huaweicloud.swagger; +import java.lang.annotation.Annotation; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -33,8 +34,11 @@ import org.springframework.beans.BeansException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; +import org.springframework.beans.factory.support.ScopeNotActiveException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.fasterxml.jackson.core.JsonProcessingException; @@ -59,7 +63,7 @@ public class ServiceCombSwaggerHandlerImpl implements ServiceCombSwaggerHandler, private ApplicationContext applicationContext; - @Value("${spring.cloud.servicecomb.swagger.enableJavaChassisAdapter:true}") + @Value("${spring.cloud.servicecomb.swagger.enableJavaChassisAdapter:false}") protected boolean withJavaChassis; private OpenApiResourceWrapper openApiResource; @@ -96,7 +100,7 @@ private void runSpringDocScanner() { } private void runJavaChassisScanner() { - Map controllers = applicationContext.getBeansWithAnnotation(RestController.class); + Map controllers = getControllers(); controllers.forEach((k, v) -> { try { SpringmvcSwaggerGenerator generator = new SpringmvcSwaggerGenerator(v.getClass()); @@ -111,6 +115,26 @@ private void runJavaChassisScanner() { swaggerSummary = calcSchemaSummary(); } + private Map getControllers() { + Map mappingsMap = new HashMap<>(); + buildControllerMapping(mappingsMap, RequestMapping.class); + buildControllerMapping(mappingsMap, RestController.class); + buildControllerMapping(mappingsMap, Controller.class); + return mappingsMap; + } + + private void buildControllerMapping(Map mappingsMap, Class clazz) { + final String[] restControllerNames = applicationContext.getBeanNamesForAnnotation(clazz); + for (String beanName : restControllerNames) { + try { + Object beanInstance = applicationContext.getBean(beanName); + mappingsMap.put(beanName, beanInstance); + } catch (ScopeNotActiveException e) { + LOGGER.warn("bean [{}] is not active in current scope, ignore it.", beanName); + } + } + } + private Map calcSchemaContent() { return swaggerMap.entrySet().stream().collect(Collectors.toMap(Entry::getKey, entry -> { try {