首页 文章

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

提问于
浏览
0

我有Spring MVC API的以下结构,带有一个 endpoints getAnnotation

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

为了测试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.thistool.MyAnalyzer]类型的限定bean [org.api .thistool.MyAnalyzer]:预计至少有1个bean可以作为此依赖项的autowire候选者 . 依赖注释:{@ 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]的限定bean [org.api.thistool.MyAnalyzer]:预计至少有一个bean有资格作为autowire候选者对于这种依赖 . 依赖注释:{@ 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 回答

  • 0

    正如评论中所讨论的,原始错误是由于使用了无效限定符 @Qualifier("MyAnalysis") . 上下文中不存在id "MyAnalysis"的bean . 由于只有一个合适的实现使用额外的 @Qualifier 是没用的 .

    第二个错误是因为 @Autowired 与构造函数的无效使用 . 类似的问题描述here

相关问题