2016-07-06 88 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

相关问题