gpt4 book ai didi

java - Spring MongoRepository,在哪里捕获异常?

转载 作者:可可西里 更新时间:2023-11-01 09:48:11 25 4
gpt4 key购买 nike

我有一个 Spring 项目和一个 MongoRepository。 MongoRepository 是一个扩展 MongoRepository 的接口(interface),就像 JPA 一样。

如果我尝试使用 mvn clean install 构建我的项目,它会运行一次 Spring。 Spring 尝试连接到未在我的 Jenkins 服务器上运行的 MongoDB。

exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.ConnectException: Connection refused: connect}

有没有办法捕获异常?我无法在我调用我的存储库的服务上捕获它,因为这些方法没有被执行。我认为它与 @autowire 有关,但我不知道如何捕获异常。

架构:

application
- resource (api)
- service
- repository extends MongoRepository

应用程序扫描项目,资源调用服务,服务调用存储库,存储库因无法连接到 MongoDB 而抛出错误。

存储库:

public interface MetingRepository extends MongoRepository<Meting, String> {
Page<Meting> findAllByRuimteId(String ruimteId, Pageable page);
}

服务:

@Service("metingenService")
public class MetingServiceImpl implements MetingService {

// could I try-catch this?
@Autowired
private MetingRepository metingRepository;

@Override
public Meting addMeting(Meting meting) {
// try-catch does not solve the issue here
return metingRepository.save(meting);
}
}
}

我唯一的测试,自动生成:

@RunWith(SpringRunner.class)
@SpringBootTest
public class MetingenServiceApplicationTests {

@Test
public void contextLoads() {

}

}

堆栈跟踪:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'metingResource': Unsatisfied dependency expressed through field 'metingService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'metingenService': Unsatisfied dependency expressed through field 'metingRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'metingRepository': Cannot resolve reference to bean 'mongoTemplate' while setting bean property 'mongoOperations'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mongoTemplate' defined in class path resource [org/springframework/boot/autoconfigure/data/mongo/MongoDataAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.mongodb.core.MongoTemplate]: Factory method 'mongoTemplate' threw exception; nested exception is org.springframework.dao.DataAccessResourceFailureException: Timed out after 30000 ms while waiting for a server that matches WritableServerSelector. Client view of cluster state is {type=UNKNOWN, servers=[{address=localhost:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.ConnectException: Connection refused: connect}}]; nested exception is com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting for a server that matches WritableServerSelector. Client view of cluster state is {type=UNKNOWN, servers=[{address=localhost:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.ConnectException: Connection refused: connect}}]

最佳答案

您的单元测试正在尝试加载完整的 Spring 上下文。因此,它会尝试加载有效的 MongoTemplate 以连接到 MongoDB 实例。

在大多数情况下,您不应该使用@SpringBootTests(用于集成测试),而是可以进行普通的 JUnit 测试:

@RunWith(JUnit4.class) // or @RunWith(MockitoJUnitRunner.class)
public class MetingenServiceApplicationTests {
...
}

关于java - Spring MongoRepository,在哪里捕获异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46885900/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com