Spring MongoRepository,在哪里捕获异常?
Spring MongoRepository, where to catch the exception?
我有一个 Spring 项目和一个 MongoRepository。 MongoRepository 是一个扩展 MongoRepository 的接口,就像 JPA 一样。
如果我尝试使用 mvn clean install
构建我的项目,它会运行 Spring 一次。 Spring 尝试连接到 MongoDB 而不是我的 Jenkins 服务器上的 运行。
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 {
...
}
我有一个 Spring 项目和一个 MongoRepository。 MongoRepository 是一个扩展 MongoRepository 的接口,就像 JPA 一样。
如果我尝试使用 mvn clean install
构建我的项目,它会运行 Spring 一次。 Spring 尝试连接到 MongoDB 而不是我的 Jenkins 服务器上的 运行。
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 {
...
}