简体   繁体   English

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

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

I have the following structure of by Spring MVC API with a single end-point getAnnotation : 我具有单个端点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)
    {
        // ...
    }
}

Interface 接口

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

Controller 控制者

@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);
    }
}

To test the API, I firstly created src/test/java/MyAnalyzerImplTest.java and was able to successfully execute it: 为了测试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);
    }

}

Now I want to run Application.java to launch API at the address http://localhost:8080 . 现在,我想运行Application.java以在地址http://localhost:8080上启动API。 I get the following error: 我收到以下错误:

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

Just in case I also provide the RAML file. 以防万一我还提供了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

As discussed in the comments, original error was due to the use of invalid qualifier @Qualifier("MyAnalysis") . 如评论中所述,原始错误是由于使用了无效的限定符@Qualifier("MyAnalysis") No bean for id "MyAnalysis" is present in the context. 上下文中不存在ID为“ MyAnalysis”的bean。 Since there's only one suitable implementation using additional @Qualifier is useless. 由于只有一种合适的实现,因此使用额外的@Qualifier是没有用的。

Second error was because of the invalid use of @Autowired with constructor. 第二个错误是由于在构造函数中无效使用了@Autowired Similiar issue is described here 这里描述类似问题

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

相关问题 Spring MVC应用程序的DAO测试时“没有合格的类型的bean” - “ No qualifying bean of type ” while DAO testing of spring mvc application Spring Data JPA-未找到符合条件的Bean - Spring data JPA - No qualifying bean found for dependency Spring MVC项目上没有任何类型的合格Bean - No qualifying bean of type on spring mvc project 未找到Spring @Autowired bean,找不到类型[...]的限定bean - Spring @Autowired bean not found, No qualifying bean of type […] found Spring MVC:未找到依赖项类型为[org.hibernate.SessionFactory]的合格Bean - Spring MVC: No qualifying bean of type [org.hibernate.SessionFactory] found for dependency 春季4中没有任何类型的合格Bean - No qualifying bean of type in Spring 4 Spring Boot:在自动装配具体类时没有“找到类型的限定bean” - Spring Boot: “No qualifying bean of type… found” when autowiring concrete class 在Spring Boot单表中找不到依赖项类型的合格Bean - No qualifying bean of type found for dependency in Spring Boot single table Spring @Autowire 失败,没有找到类型的合格 bean 依赖错误 - Spring @Autowire fails with No qualifying bean of type found for dependency error Spring Data JPA没有类型的限定bean ...找到依赖项 - Spring Data JPA No qualifying bean of type … found for dependency
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM