2016-11-09 1 views
1

メインクラスと春ブーツ使い方:残りコントローラ

package com.rsc.springboot_test; 
@SpringBootApplication 
public class SpringBootTest { 
    public static void main(String[] args) { 
     SpringApplication.run(SpringBootTest.class, args); 
    } 
} 

コントローラクラス:

package com.rsc.springboot_test_controller; 
@RestController 
public class testController { 

    @RequestMapping("/home") 
    public void home() { 
     System.out.println("@@@Home Controller Called"); 
    } 
} 

POMファイル:

<?xml version="1.0" encoding="UTF-8"?> 
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 

    <groupId>com.mycompany</groupId> 
    <artifactId>springboot_test</artifactId> 
    <version>1.0-SNAPSHOT</version> 
    <packaging>war</packaging> 

    <name>springboot_test</name> 

    <properties> 
     <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir> 
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
     <java.version>1.8</java.version> 
     <jjwt.version>0.7.0</jjwt.version> 
    </properties> 

    <parent> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-parent</artifactId> 
     <version>1.4.2.RELEASE</version> 
     <relativePath/> <!-- lookup parent from repository --> 
    </parent> 

    <dependencies> 
     <dependency> 
      <groupId>javax</groupId> 
      <artifactId>javaee-web-api</artifactId> 
      <version>7.0</version> 
      <scope>provided</scope> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-data-jpa</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-data-rest</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-jdbc</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-security</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-web</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>mysql</groupId> 
      <artifactId>mysql-connector-java</artifactId> 
      <scope>runtime</scope> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-test</artifactId> 
      <scope>test</scope> 
     </dependency> 
     <dependency> 
      <groupId>commons-dbcp</groupId> 
      <artifactId>commons-dbcp</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.security.oauth</groupId> 
      <artifactId>spring-security-oauth2</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>io.jsonwebtoken</groupId> 
      <artifactId>jjwt</artifactId> 
      <version>${jjwt.version}</version> 
     </dependency> 
     <!-- https://mvnrepository.com/artifact/org.yaml/snakeyaml --> 
     <dependency> 
      <groupId>org.yaml</groupId> 
      <artifactId>snakeyaml</artifactId> 
      <version>1.17</version> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.mobile</groupId> 
      <artifactId>spring-mobile-device</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>com.h2database</groupId> 
      <artifactId>h2</artifactId> 
      <scope>runtime</scope> 
     </dependency> 
    </dependencies> 

    <build> 
     <plugins> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-compiler-plugin</artifactId> 
       <version>3.1</version> 
       <configuration> 
        <source>1.7</source> 
        <target>1.7</target> 
        <compilerArguments> 
         <endorseddirs>${endorsed.dir}</endorseddirs> 
        </compilerArguments> 
       </configuration> 
      </plugin> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-war-plugin</artifactId> 
       <version>2.3</version> 
       <configuration> 
        <failOnMissingWebXml>false</failOnMissingWebXml> 
       </configuration> 
      </plugin> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-dependency-plugin</artifactId> 
       <version>2.6</version> 
       <executions> 
        <execution> 
         <phase>validate</phase> 
         <goals> 
          <goal>copy</goal> 
         </goals> 
         <configuration> 
          <outputDirectory>${endorsed.dir}</outputDirectory> 
          <silent>true</silent> 
          <artifactItems> 
           <artifactItem> 
            <groupId>javax</groupId> 
            <artifactId>javaee-endorsed-api</artifactId> 
            <version>7.0</version> 
            <type>jar</type> 
           </artifactItem> 
          </artifactItems> 
         </configuration> 
        </execution> 
       </executions> 
      </plugin> 
     </plugins> 
    </build> 
</project> 

その残りのコントローラを備えたシンプルな春ブーツアプリを。私はそれが出力System.out.println("@@@Home Controller Called");を印刷していないhttp://localhost:8084/springboot_test/homeを呼び出すときhttp://localhost:8084/springboot_test/負荷に問題があり、デフォルトのindex.htmlページ

を呼び出す

はので、私は、要求が残り、コントローラ自体で呼び出されていないと思います。しかし、なぜそれが呼ばれていないのですか?

更新

@javaguy作品によって提案されたが、私はそれが起動時にすべてのパッケージを走査するようにメインクラスの上に@componentScanを適用すると、残りコントローラの@ComponentScan("com.rsc.springboot_test")を使用します。次のことをしようと

はエラーを与える:

@SpringBootApplication 
@ComponentScan(basePackages = { "com.rsc.*" }) 
public class SpringBootTest { 
    public static void main(String[] args) { 
     SpringApplication.run(SpringBootTest.class, args); 
    } 
} 

エラー:

java.lang.IllegalStateException: ApplicationEventMulticaster not initialized - call 'refresh' before multicasting events via the context: org.springframework.boot[email protected]58f7ff2f: startup date [Wed Nov 09 21:08:20 IST 2016]; root of context hierarchy 
    at org.springframework.context.support.AbstractApplicationContext.getApplicationEventMulticaster(AbstractApplicationContext.java:404) [spring-context-4.3.4.RELEASE.jar:4.3.4.RELEASE] 
    at org.springframework.context.support.ApplicationListenerDetector.postProcessBeforeDestruction(ApplicationListenerDetector.java:97) ~[spring-context-4.3.4.RELEASE.jar:4.3.4.RELEASE] 
    at org.springframework.beans.factory.support.DisposableBeanAdapter.destroy(DisposableBeanAdapter.java:253) ~[spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE] 
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroyBean(DefaultSingletonBeanRegistry.java:578) [spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE] 
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroySingleton(DefaultSingletonBeanRegistry.java:554) [spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE] 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.destroySingleton(DefaultListableBeanFactory.java:954) [spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE] 
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroySingletons(DefaultSingletonBeanRegistry.java:523) [spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE] 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.destroySingletons(DefaultListableBeanFactory.java:961) [spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE] 


2016-11-09 21:08:20.765 ERROR 8500 --- [o-8084-exec-402] o.s.boot.SpringApplication    : Application startup failed 

org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [com.rsc.springboot_test.SpringBootTest]; nested exception is org.springframework.context.annotation.ConflictingBeanDefinitionException: Annotation-specified bean name 'testController' for bean class [com.rsc.springboot_test_controller.testController] conflicts with existing, non-compatible bean definition of same name and class [com.rsc.springboot_test.controller.testController] 
    at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:180) ~[spring-context-4.3.4.RELEASE.jar:4.3.4.RELEASE] 
    at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:324) ~[spring-context-4.3.4.RELEASE.jar:4.3.4.RELEASE] 
    at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:246) ~[spring-context-4.3.4.RELEASE.jar:4.3.4.RELEASE] 
    at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:270) ~[spring-context-4.3.4.RELEASE.jar:4.3.4.RELEASE] 
    at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:93) ~[spring-context-4.3.4.RELEASE.jar:4.3.4.RELEASE] 

    at java.lang.Thread.run(Thread.java:745) [na:1.8.0_102] 
Caused by: org.springframework.context.annotation.ConflictingBeanDefinitionException: Annotation-specified bean name 'testController' for bean class [com.rsc.springboot_test_controller.testController] conflicts with existing, non-compatible bean definition of same name and class [com.rsc.springboot_test.controller.testController] 
    at org.springframework.context.annotation.ClassPathBeanDefinitionScanner.checkCandidate(ClassPathBeanDefinitionScanner.java:320) ~[spring-context-4.3.4.RELEASE.jar:4.3.4.RELEASE] 
    at org.springframework.context.annotation.ClassPathBeanDefinitionScanner.doScan(ClassPathBeanDefinitionScanner.java:259) ~[spring-context-4.3.4.RELEASE.jar:4.3.4.RELEASE] 
    at org.springframework.context.annotation.ComponentScanAnnotationParser.parse(ComponentScanAnnotationParser.java:137) ~[spring-context-4.3.4.RELEASE.jar:4.3.4.RELEASE] 
    at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:268) ~[spring-context-4.3.4.RELEASE.jar:4.3.4.RELEASE] 
    at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:230) ~[spring-context-4.3.4.RELEASE.jar:4.3.4.RELEASE] 
09-Nov-2016 21:08:21.046 SEVERE [http-nio-8084-exec-402] org.apache.catalina.core.ContainerBase.addChildInternal ContainerBase.addChild: start: 
org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/springboot_test]] 
    at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:197) ~[spring-context-4.3.4.RELEASE.jar:4.3.4.RELEASE] 
    at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:166) ~[spring-context-4.3.4.RELEASE.jar:4.3.4.RELEASE] 
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154) 
    ... 61 common frames omitted 

    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:725) 

Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [com.rsc.springboot_test.SpringBootTest]; nested exception is org.springframework.context.annotation.ConflictingBeanDefinitionException: Annotation-specified bean name 'testController' for bean class [com.rsc.springboot_test_controller.testController] conflicts with existing, non-compatible bean definition of same name and class [com.rsc.springboot_test.controller.testController] 
    at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:180) 
    at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:324) 
    at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:246) 
    at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:270) 

Caused by: org.springframework.context.annotation.ConflictingBeanDefinitionException: Annotation-specified bean name 'testController' for bean class [com.rsc.springboot_test_controller.testController] conflicts with existing, non-compatible bean definition of same name and class [com.rsc.springboot_test.controller.testController] 
    at org.springframework.context.annotation.ClassPathBeanDefinitionScanner.checkCandidate(ClassPathBeanDefinitionScanner.java:320) 


09-Nov-2016 21:08:21.200 SEVERE [http-nio-8084-exec-402] org.apache.catalina.startup.HostConfig.deployDescriptor Error deploying configuration descriptor C:\Users\Documents\AppData\Roaming\NetBeans\8.2\apache-tomcat-8.0.27.0_base\conf\Catalina\localhost\springboot_test.xml 
java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/springboot_test]] 
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:729) 
+1

代わりに 'http:// localhost:8084/home'を呼ぶべきではありませんか? – Maroun

+0

@MarounMarounいいえ、 'http:// localhost:8084/springboot_test /'を呼び出すとデフォルトのindex.htmlページがロードされるので、 'springboot_test /'の後のものはrest controllerの呼び出しですと思っています – Satyadev

+0

SpringBootTestのパッケージ名クラス?パッケージの詳細を追加する – developer

答えて

2

あなたRestControllerはSpringコンテナで検出されたので、あなたはComponentScanにパッケージの詳細を追加する必要があるので、変更されていませんmain:

@SpringBootApplication 
@ComponentScan(basePackages = { "com.rsc" }) 
public class SpringBootTest { 
    public static void main(String[] args) { 
    SpringApplication.run(SpringBootTest.class, args); 
    } 
} 

また、テストアプリケーションでは、あなたは以下のパスで1が設定されているように、ED(私たちは、アノテーションを使用してcomponentscanを使用してコンポーネントをスキャンすると)任意のXML、思わ:

C:\Users\Documents\AppData\Roaming\NetBeans\8.2\apache-tomcat-8.0.27.0_base\conf\Catalina\localhost\springboot_test.xml 

サーバを削除して再起動する必要があります。

+0

あなたは正しいです。しかし、アプリケーションで定義した残りのすべてのコントローラに対して@ ComponentScanを呼び出す必要があります。グローバルコンポーネントスキャンはできませんか? – Satyadev

+0

はい、私の答えを編集しました、それはSpringBootTestクラスの上にあります – developer

+0

そうです。ありがとうございます – Satyadev

0

あなたの問題は、あなたのApplicationクラスがComponentScanであることです。あなたのプロジェクト構造は次のようになります。

src 
    main 
    java 
     com.company 
     Application.java 
     controller 
      MyController.java 
     service 
     repository 

そして、あなたのアプリケーション:

@SpringBootApplication 
@ComponentScan(basePackages = { 
    "com.rsc.springboot_test_controller", 
    "com.rsc.springboot_test_service", 
    //etc... no ' at the end of the last package 
}) 
public class SpringBootTest { 
    public static void main(String[] args) { 
     SpringApplication.run(SpringBootTest.class, args); 
    } 
} 

実際には上記の2つの異なるアプローチの混合物であるが、動作します。 Application.javaは、他のすべてのパッケージのルートパッケージに含まれている必要があります。

、もう一度あなたの質問を読んで、私はControllerを変更することをお勧めします:デフォルトで

package com.rsc.springboot_test_controller; 
@RestController 
public class testController { 

    @RequestMapping("/home") 
    public String home() { 
     return "@@@Home Controller Called"; 
    } 
} 
1

@SpringBootApplicationは、注釈付きクラスとすべてのサブパッケージと同じパッケージでコンポーネントのスキャンを開始します。

メインクラスがcom.rsc.springboot_testでコントローラがcom.rsc.springboot_test_controllerであるため、TestControllerクラスはスキャンされません。を推奨

  • :それは、デフォルトのクラスパスのスキャンと一致するように他のパッケージをスキャンする@SpringBootApplicationを変更com.rsc.springboot_test.controller
  • に、たとえば、別のパッケージにTestControllerを移動:それを解決するには、2つのアプローチがある

@SpringBootApplication(scanBasePackages = { 
     "com.rsc.springboot_test", 
     "com.rsc.springboot_test_controller" 
}) 
関連する問題