2016-07-06 79 views
0

我有以下結構由Spring MVC API與單一終點getAnnotation測試Spring MVC的API:沒有合格的豆發現

@SpringBootApplication 
public class Application { 
    @Bean 
    public javax.validation.Validator localValidatorFactoryBean() { 
     return new LocalValidatorFactoryBean(); 
    } 

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

} 


@Service 
public class MyAnalyzerImpl implements MyAnalyzer 
{ 

    @Autowired 
    public MyAnalyzerImpl() {} 

    @Override 
    public Annotations getAnnotation(MyRequest request) 
    { 
     // ... 
    } 
} 

接口

public interface MyAnalyzer { 
    Annotations getAnnotation(MyRequest request); 
} 

控制器

@RestController 
@RequestMapping("/thisapi/{id}") 
public class MyController { 

    @Autowired 
    @Qualifier("MyAnalysis") 
    MyAnalyzer myAnalyzer; 

    @RequestMapping(value = "/getAnnotation", method = RequestMethod.GET) 
    public Annotations getAnnotation(@PathVariable String docId, 
            @RequestParam(value = "document", defaultValue = "{'id':'1','title':'bla-bla'}") String text) { 
     MysRequest myRequest = new MyRequest(MyRequest.TYPE_ANNOTATION, text); 
     return myAnalyzer.getAnnotation(myRequest); 
    } 
} 

要測試API,我首先創建src/test/java/MyAnalyzerImplTest.java,並能成功地執行它:

@RunWith(SpringJUnit4ClassRunner.class) 
public class MyAnalyzerImplTest { 

    private MyAnalyzerImpl myAnalyzer; 
    private String sampleText; 

    @Test 
    public void testEndpoint() throws Exception { 
     MyRequest request = new MyRequest( MyRequest.TYPE_ANNOTATION, 
                "1", 
                sampleText 
               ); 
     Annotations results = myAnalyzer.getAnnotation(request); 
     Assert.assertTrue("This " + results.getPayload().getWords().size() + ") " + 
       "should be greater than 0", results.getPayload().getWords().size() > 0); 
    } 

    @Before 
    public void setUp() throws Exception { 
     myAnalyzer = new MyAnalyzerImpl(); 
     File f = new File("src/test/resources/texsts/text.json"); 
     if (f.exists()){ 
      InputStream is = new FileInputStream("src/test/resources/texts/text.json"); 
      samplePublication = IOUtils.toString(is); 
     } 
     Thread.sleep(1000); 
    } 

} 

現在我想運行Application.java到該地址http://localhost:8080推出API。我得到以下錯誤:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'myController': Unsatisfied dependency expressed through field 'myAnalyzer': No qualifying bean of type [org.api.thistool.MyAnalyzer] found for dependency [org.api.thistool.MyAnalyzer]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=MyAnalysis)}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.api.thistool.MyAnalyzer] found for dependency [org.api.thistool.MyAnalyzer]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=MyAnalysis)}

以防萬一我也提供RAML文件。

#%RAML 0.8 
title: MY API 
version: v1 
baseUri: http://localhost:8080 
resourceTypes: 
    - annotate-type: 
     description: Bla-bla 
     get: 
     description: bla-bla 
     queryParameters: 
      text: 
      description: json of a document 
      type: string 
      required: true 
      default: "{'id':'1','title':'bla-bla'}" 
     responses: 
      200: 
      body: 
       application/json: 
       example: | 
        { 
        "words": "['aaa', 'bbb']" 
        } 
/thisapi: 
    /{id}/getAnnotation: 
     type: 
      annotate-type: 
     uriParameters: 
      id: 
      description: document id 
      type: string 
+1

春天不能自動裝配依賴於「MyAnalyzer」,因爲它無法找到ID爲「MyAnalysis」的bean。如果上下文中只有一個該類型的bean,則可以刪除@Qualifier(「MyAnalysis」)。如果有型MyAnalysis的多個豆加適量@Qualifier爲MyAnalyzerImpl – jp86

+0

@ jp86:你的意思是我應該使用'@Autowired MyAnalyzer myAnalyzer;',而不是'@Autowired @Qualifier ( 「MyAnalysis」) MyAnalyzer myAnalyzer;' ? – Klue

+0

是的,它應該有所幫助。我將嘗試解釋:您的控制器需要一個類型爲MyAnalyzer的_single_ bean,因此您在該依賴項上使用'@ Autowired'。這將指示spring在上下文中查找該類型的bean並自動注入它。當你還聲明'@ Qualifier'時,它會將掃描限制爲具有該ID的bean,在這種情況下爲「MyAnalysis」。當應用程序啓動'MyAnalyzerImpl'將(默認),獲得豆ID「myAnalyzerImpl」沒有進一步的指令,因此你看到的「不滿意依賴性表達......」正在自動裝配依賴時出錯。 – jp86

回答

0

正如評論中所述,原始錯誤是由於使用無效限定符@Qualifier("MyAnalysis")造成的。上下文中不存在用於標識「MyAnalysis」的bean。由於只有一個合適的實現使用額外的@Qualifier是沒有用的。

二錯誤是因爲無效使用@Autowired與構造。 Similiar問題描述here

相關問題