spring-boot/@jdbctest-com.fasterxml.jackson.databind.objectmapper类型的合格bean不可用

zte4gxcn  于 2021-07-08  发布在  Java
关注(0)|答案(0)|浏览(411)

有点新秀在这里,所以感谢任何帮助!
我已经用jdbctemplate构建了一个spring引导应用程序,它可以正常运行,没有任何错误或异常。
我已经生成了一个测试类,并希望使用@jdbctest来测试我的dao对象。但是,每次我运行测试时 java.lang.IllegalStateException: Failed to load ApplicationContext . 我的控制器类似乎有问题。此illegalstateexception由以下原因引起:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'assetApiController' defined in file [/PATH/TO/FILE]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.fasterxml.jackson.databind.ObjectMapper' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}

我的测试类:

@JdbcTest
@Sql({"schema.sql", "test-data.sql"})
class AssetApiControllerTest {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Autowired
    AssetDao assetDao;

    @Test
    void delete() throws DataAccessException {
        assetDao.setJdbcTemplate(jdbcTemplate);

        assetDao.deleteByPk(new AssetKey(1), null);
        assertEquals(0, assetDao.selectAll(null).size());
    }
}

我的刀:

@Repository("assetDao")
public class AssetDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    public void deleteByPk (AssetKey assetKey, Connection con) throws DataAccessException {

        jdbcTemplate.update("DELETE FROM Asset WHERE id = ? ", assetKey.getId());
    }
   }

我的控制器类:

@Controller
public class AssetApiController implements AssetApi {

    private static final Logger log = LoggerFactory.getLogger(AssetApiController.class);

    private final ObjectMapper objectMapper;

    private final HttpServletRequest request;

    @Autowired
    private AssetDao assetDao;

    @org.springframework.beans.factory.annotation.Autowired
    public AssetApiController(ObjectMapper objectMapper, HttpServletRequest request) {
        this.objectMapper = objectMapper;
        this.request = request;
    }

    @Override
    public ResponseEntity<Void> delete(@RequestBody AssetKey assetKey) {
        try{
            assetDao.deleteByPk(assetKey, null);
        } catch (ApplicationException e) {
            log.warn(e.getFormattedMessage(), e);
            return new ResponseEntity<Void>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
        return new ResponseEntity<Void>(HttpStatus.OK);
    }

 }

我的jacksonconfig:

@Configuration
public class JacksonConfiguration {

  @Bean
  @ConditionalOnMissingBean(ThreeTenModule.class)
  ThreeTenModule threeTenModule() {
    ThreeTenModule module = new ThreeTenModule();
    module.addDeserializer(Instant.class, CustomInstantDeserializer.INSTANT);
    module.addDeserializer(OffsetDateTime.class, CustomInstantDeserializer.OFFSET_DATE_TIME);
    module.addDeserializer(ZonedDateTime.class, CustomInstantDeserializer.ZONED_DATE_TIME);
    return module;
  }

}

还有两点可以帮助诊断:
我遵循这个答案:https://stackoverflow.com/a/32842962/11853066 . 它解决了这个问题,但是我得到了httpservletrequest的一个等效错误: No qualifying bean found for dependency [javax.servlet.http.HttpServletRequest] . 所以根本问题没有得到解决。
当我尝试为资产以外的实体(例如带有suppliercontroller、supplierdao的“supplier”)生成测试时,我会得到相同的错误: Error creating bean with name 'assetApiController' defined in file . 这一定是因为assetapicontroller是第一个按字母顺序扫描的控制器?
谢谢你的帮助!

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题