Использование разных пакетов для контроллеров при использовании в Spring Boot - PullRequest
0 голосов
/ 28 августа 2018

Я новичок в весенней загрузке. Я пытаюсь получить доступ к URL контроллера, но я не получил от них никакого ответа.

Мы соблюдаем структуру проекта весенней загрузки согласно документу. Я использовал компонентную аннотацию и добавил scanBasePackageClasses в аннотацию SpringBootApplication. Но все так же.

HomeController: -

package com.main.controllers;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class HomeController {
    @RequestMapping(value="/home",method=RequestMethod.GET)
    public String home() {
        System.out.println("home");
        return "home";
    }
}

GAppsApplication.java: -

package com.main;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import com.main.controllers.HomeController;
@ComponentScan({"com.main.controllers"})
@SpringBootApplication(scanBasePackageClasses={HomeController.class})
public class GAppsApplication {

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

Для справки я поделился ниже журналами журналы: -

2018-08-28 08:26:16.404  INFO 8172 --- [  restartedMain] o.s.s.web.DefaultSecurityFilterChain     : Creating filter chain: org.springframework.security.web.util.matcher.AnyRequestMatcher@1, [org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter@58b9d6, org.springframework.security.web.context.SecurityContextPersistenceFilter@525747, org.springframework.security.web.header.HeaderWriterFilter@1638752, org.springframework.security.web.csrf.CsrfFilter@324c61, org.springframework.security.web.authentication.logout.LogoutFilter@14a6743, org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter@12d6ce4, org.springframework.security.web.authentication.ui.DefaultLoginPageGeneratingFilter@1837d01, org.springframework.security.web.authentication.www.BasicAuthenticationFilter@eda2d2, org.springframework.security.web.savedrequest.RequestCacheAwareFilter@4a5109, org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter@4bb38c, org.springframework.security.web.authentication.AnonymousAuthenticationFilter@12205be, org.springframework.security.web.session.SessionManagementFilter@2e4343, org.springframework.security.web.access.ExceptionTranslationFilter@1a81a58, org.springframework.security.web.access.intercept.FilterSecurityInterceptor@12e7437]
2018-08-28 08:26:16.558  INFO 8172 --- [  restartedMain] o.s.b.d.a.OptionalLiveReloadServer       : LiveReload server is running on port 35729
2018-08-28 08:26:16.592  INFO 8172 --- [  restartedMain] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2018-08-28 08:26:16.594  INFO 8172 --- [  restartedMain] o.s.j.e.a.AnnotationMBeanExporter        : Bean with name 'dataSource' has been autodetected for JMX exposure
2018-08-28 08:26:16.600  INFO 8172 --- [  restartedMain] o.s.j.e.a.AnnotationMBeanExporter        : Located MBean 'dataSource': registering with JMX server as MBean [com.zaxxer.hikari:name=dataSource,type=HikariDataSource]
2018-08-28 08:26:16.638  INFO 8172 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2018-08-28 08:26:16.642  INFO 8172 --- [  restartedMain] c.main.GAppsApplication                  : Started GAppsApplication in 5.153 seconds (JVM running for 5.734)
2018-08-28 08:27:11.961  INFO 8172 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring FrameworkServlet 'dispatcherServlet'
2018-08-28 08:27:11.961  INFO 8172 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization started
2018-08-28 08:27:11.980  INFO 8172 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization completed in 19 ms

1 Ответ

0 голосов
/ 28 августа 2018

Измените ваш контроллер, как показано ниже:

@RestController
@RequestMapping("/home")
public class HomeController {
    @GetMapping
    public String home() {
        System.out.println("home");
        return "home";
    }
}

Поскольку ваш базовый пакет com.main, то любой пакет в com.main будет автоматически сканироваться.

Вам не нужно помещать какие-либо специальные аннотации для сканирования.

...