繁体   English   中英

测试Spring MVC API:未找到合格的bean

[英]Testing Spring MVC API: No qualifying bean found

我具有单个端点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:创建名称为“ myController”的bean时出错:通过字段“ myAnalyzer”表达的不满意依赖关系:未找到依赖项[org.api]的类型为[org.api.thistool.MyAnalyzer]的合​​格Bean。 [thistool.MyAnalyzer]:期望至少有1个bean符合此依赖项的自动装配条件。 依赖注释:{@ org.springframework.beans.factory.annotation.Autowired(required = true),@ org.springframework.beans.factory.annotation.Qualifier(value = MyAnalysis)}; 嵌套的异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:未找到依赖项[org.api.thistool.MyAnalyzer]的类型为[org.api.thistool.MyAnalyzer]的合​​格Bean:至少需要1个符合自动装配候选条件的bean对于这种依赖性。 依赖项注释:{@ 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

如评论中所述,原始错误是由于使用了无效的限定符@Qualifier("MyAnalysis") 上下文中不存在ID为“ MyAnalysis”的bean。 由于只有一种合适的实现,因此使用额外的@Qualifier是没有用的。

第二个错误是由于在构造函数中无效使用了@Autowired 这里描述类似问题

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM