Java - Multimoduling Spring Boot Gradle - PullRequest
0 голосов
/ 22 февраля 2020

У меня есть многомодульный проект с Gradle, который использует следующую структуру:

    transportapp
    ├── transportapp-adapters
    |   ├── transportapp-persistence
    |   |  ├── src
    |   |  └── build.gradle
    ├── transportapp-infrastructure
    |   ├── src
    |   └── build.gradle
    ├── transportapp-application
    |   ├── transportapp-apis
    |   |   ├── transportapp-admin-api
    |   |   |   ├── src
    |   |   |   └── build.gradle
    |   |   ├── transportapp-customers-api
    |   |   |   ├── src
    |   |   |   └── build.gradle
    ├── transportapp-common
    |   ├── src
    |   └── build.gradle
    ├── transportapp-configuration
    |   ├── src
    |   └── build.gradle
    ├── build.gradle
    └── settings.gradle

У меня есть все мои приложения конфигурации (основной класс, конфигурация класса бобов) в transportapp-configuration с другой стороны, у меня есть мои остальные контроллеры в transportapp-admin-api и transportapp-Customers-api .

Мне нужно запустить приложение весенней загрузки, но когда я пытаюсь получить доступ к моим методам отдыха, которые определены в контроллере отдыха, я получаю ошибку 404.

Ответ на запрос, сделанный через Почтальона:

    {
      "timestamp": "2020-02-21T23:31:53.052+0000",
      "status": 404,
      "error": "Not Found",
      "message": "No message available",
      "path": "/companies/create"
    }

Первоначально был определен основной класс со следующим кодом:

    @SpringBootApplication
    public class TransportappJavaServiceApplication {

        public static void main(String[] args) {
            SpringApplication.run(EnrutappJavaServiceApplication.class, args);              
        }

    }

С этим кодом приложение весенней загрузки запускается правильно, но у меня возникает ошибка 404, когда я пытаюсь получить доступ к ресурсам моих остальных контроллеров, исследуя различные посты, я обнаружил, что я также может использовать @ComponentScan, но с этой аннотацией приложение весенней загрузки не может запуститься и вернуть ошибку.

Последняя модификация основного класса с использованием аннотации @ComponentScan выглядит так:

    @SpringBootApplication
    @ComponentScan(basePackages = "com.transportapp")
    public class TransportappJavaServiceApplication {

        public static void main(String[] args) {
            SpringApplication.run(EnrutappJavaServiceApplication.class, args);              
        }

    }

Это ошибка журнала:

org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1312) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE]
    ... 17 common frames omitted
Caused by: java.lang.Error: Unresolved compilation problems: 
    The import org.springframework cannot be resolved
    The import org.springframework cannot be resolved
    The import org.springframework cannot be resolved
    The import org.springframework cannot be resolved
    The import org.springframework cannot be resolved
    The import org.springframework cannot be resolved
    The import org.springframework cannot be resolved
    The import org.springframework cannot be resolved
    The import com.transportapp.common cannot be resolved
    The import com.transportapp.common cannot be resolved
    The import com.transportapp.infrastructure cannot be resolved
    RestController cannot be resolved to a type
    RequestMapping cannot be resolved to a type
    CrossOrigin cannot be resolved to a type
    RequestMethod cannot be resolved to a variable
    RequestMethod cannot be resolved to a variable
    RequestValidator cannot be resolved to a type
    Autowired cannot be resolved to a type
    CompanyServiceImpl cannot be resolved to a type
    GetMapping cannot be resolved to a type
    Company cannot be resolved to a type
    CompanyServiceImpl cannot be resolved to a type
    PostMapping cannot be resolved to a type
    RequestBody cannot be resolved to a type
    The method validateRequest(CompanyRequest) is undefined for the type CompanyResource
    Company cannot be resolved to a type
    The method getCompany() from the type CompanyRequest refers to the missing type Company
    CompanyServiceImpl cannot be resolved to a type

    at com.transportapp.application.services.admin.http.resources.CompanyResource.<init>(CompanyResource.java:6) ~[default/:na]
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_221]
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) ~[na:1.8.0_221]
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) ~[na:1.8.0_221]
    at java.lang.reflect.Constructor.newInstance(Unknown Source) ~[na:1.8.0_221]
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:200) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE]
    ... 19 common frames omitted

Этот RestController CompanyResource. java выглядит так:

    @RestController
    @RequestMapping("companies")
    @CrossOrigin(origins = "*", methods= {RequestMethod.GET,RequestMethod.POST})
    public class CompanyResource extends RequestValidatorServiceImpl{

       @Autowired
       private CompanyServiceImpl companyService;

       @GetMapping(value = "/get")
       public CompanyResponse getAllCompanies() {
           CompanyResponse response = new CompanyResponse();
           response.setSuccess(true);
           response.setMessage("Showing the list of created companies in database");
           try {
               List<Company> createdCompanies = companyService.findAllCompanies();
               response.setCompanies(createdCompanies);
               response.setCountCompanies(createdCompanies.size());
           }catch (InternalError ex) {
               response.setSuccess(false);
               response.setMessage(ex.getMessage());
           }
           return response;
       }

       @PostMapping(value = "/create")      
       public CompanyResponse createCompany(@RequestBody CompanyRequest request) {

           CompanyResponse response = new CompanyResponse();

           response.setSuccess(true);
           response.setRequest(request);        
           response.setMessage("A new company has been successful created");            
           try {                 
               validateRequest(request);
               Company company = request.getCompany();
               companyService.create(company);          
           }catch(InternalError ex) {
               response.setSuccess(false);
               response.setMessage(ex.getMessage());
           }
           return response;
       }
    }

Что я могу сделать, чтобы предотвратить ошибку и запустить мое приложение со всеми ресурсами, которые определены в моих остальных контроллерах?

...