java - Spring数据JPA : org. springframework.beans.factory.UnsatisfiedDependencyException:

标签 java spring spring-boot junit4

我正在尝试在 Application.property 文件中添加 crossOrigin 变量。并使用 @value 通过 Controller 访问该变量,它适用于以下两个端点,但是当我将 @value 添加到第三个端点时,我收到以下错误消息

AccountController.java

@RestController
@RequestMapping("/api.spacestudy.com/SpaceStudy/Admin/Account")
public class AccountController {

    @Autowired
    AccountService accService;

    @Value("${crossOrigin}")
    @GetMapping("/loadAcctLocationList")
    public ResponseEntity<List<Account>> findLocation() {
        return  ResponseEntity.ok(accService.findLocation());
    }

}

TestAccountController

@RunWith(SpringRunner.class)
public class TestAccountController {

    private MockMvc mockMvc;

    @Mock
    private AccountService accountService;

    @InjectMocks
    private AccountController accountController;

    @Before
    public void setup() {
        mockMvc = MockMvcBuilders.standaloneSetup(accountController).build();
    }

    @Test
    public void findLocationTest() throws Exception {

        Account account = new Account();
        account.setsLocation("Test1");

        List<Account> accountObj = new ArrayList<Account>();
        accountObj.add(account);    

        Mockito.when(accountService.findLocation()).thenReturn(accountObj);

        mockMvc.perform(get("/spacestudy/$ InstituteIdentifier/admin/account/loadAcctLocationList"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$[0].sLocation", is("Test1")))
                .andExpect(jsonPath("$.*",Matchers.hasSize(1)));    

        for(Account result: accountObj) {

            assertEquals("Test1", result.sLocation);

        }

        }

application.properties

crossOrigin =@CrossOrigin(origins ="http://localhost:4200")
server.port=8086
spring.datasource.url=jdbc:postgresql://localhost/SpaceStudyDB
spring.datasource.username=postgres
spring.datasource.password=postgres

控制台

Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
2018-05-15 01:59:17.915 ERROR 5240 --- [           main] o.s.boot.SpringApplication               : Application startup failed

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'accountController': Unsatisfied dependency expressed through method 'btnSaveClick' parameter 0; nested exception is org.springframework.beans.ConversionNotSupportedException: Failed to convert value of type 'java.lang.String' to required type 'com.spacestudy.model.AccountMaintenanceSave'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'com.spacestudy.model.AccountMaintenanceSave': no matching editors or conversion strategy found
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.inject(AutowiredAnnotationBeanPostProcessor.java:667) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:366) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1264) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867) ~[spring-context-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543) ~[spring-context-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693) [spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360) [spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:303) [spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118) [spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107) [spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
    at com.spacestudy.SpaceStudyAdminAccountMaintenance.main(SpaceStudyAdminAccountMaintenance.java:10) [classes/:na]
Caused by: org.springframework.beans.ConversionNotSupportedException: Failed to convert value of type 'java.lang.String' to required type 'com.spacestudy.model.AccountMaintenanceSave'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'com.spacestudy.model.AccountMaintenanceSave': no matching editors or conversion strategy found
    at org.springframework.beans.TypeConverterSupport.doConvert(TypeConverterSupport.java:74) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.beans.TypeConverterSupport.convertIfNecessary(TypeConverterSupport.java:47) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1093) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.inject(AutowiredAnnotationBeanPostProcessor.java:659) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    ... 19 common frames omitted
Caused by: java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'com.spacestudy.model.AccountMaintenanceSave': no matching editors or conversion strategy found
    at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:307) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:109) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.beans.TypeConverterSupport.doConvert(TypeConverterSupport.java:64) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    ... 23 common frames omitted

谁能告诉我上面代码中我做错了什么?或者告诉我在 application.property 文件中声明变量的任何其他方法

最佳答案

看起来 Spring 在这里试图做的是将 ${crossOrigin} 的值注入(inject)到您的方法中。

  1. findLocation 中,它不会中断,因为没有可注入(inject)值的方法参数
  2. btnSearchClick 中,有多个参数,并且它不会尝试注入(inject)值
  3. 有一个参数,它尝试将 ${crossOrigin} 的值注入(inject)到 AccountMaintenanceSave saveObj

您应该将 ${crossOrigin} 注入(inject)到类顶部的 @CrossOrigin 中吗?就像这样:

@RestController
@CrossOrigin(origins = "${crossOrigin}")
@RequestMapping("/api.spacestudy.com/SpaceStudy/Admin/Account")
public class AccountController {

    @Autowired
    AccountService accService;

    ...
}

并以这种方式访问​​变量。您需要从 Controller 方法中删除 @Value 注释。

您还需要在 application.properties 中指定原始名称:

crossOrigin=http://localhost:4200

关于java - Spring数据JPA : org. springframework.beans.factory.UnsatisfiedDependencyException:,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50352626/

相关文章:

java - 如何使用jdbc设置实例名称

java - 针对 Java EE 6 API 进行测试

java - 使用spring mvc时jsp页面出错

java - Hibernate @OneToMany 映射给出异常,

json - 从 spring boot 以 angular 4 发布上传文件?

java - 如何在没有 xml 的情况下配置 Ehcache 3 + spring boot + java config?

java - 当对象中的所有字段都为空时,如何优雅地进行验证?

java - 访问 DefaultScheduleEvent 数据对象

java - richer如何适配服务激活器和网关接口(interface)?

java - 从命令行通过 SSL 使用 Spring HttpInvoker