英文:
Error when trying to send data from the spring application to mongodb on remote server
问题
Sure, here's the translated content you provided:
我正在尝试创建一个连接到外部主机上数据库的网站。为此,我创建了两个虚拟机。其中一个包含MongoDB数据库,另一个包含在spring-boot + react中创建的网站。我目前正在创建RestController的阶段。在尝试发送测试请求时,我遇到了以下错误:
2020-10-25 17:39:38.492 ERROR 25849 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.data.mongodb.UncategorizedMongoDbException: Exception authenticating MongoCredential{mechanism=SCRAM-SHA-1, userName='', source='', password=<hidden>, mechanismProperties=<hidden>}; nested exception is com.mongodb.MongoSecurityException: Exception authenticating MongoCredential{mechanism=SCRAM-SHA-1, userName='', source='', password=<hidden>, mechanismProperties=<hidden>}] with root cause
java.lang.IllegalArgumentException: state should be: databaseName is not empty
at com.mongodb.assertions.Assertions.isTrueArgument(Assertions.java:101) ~[mongodb-driver-core-4.0.5.jar:na]
...
**我使用Swagger测试请求。**
据我所了解,应用程序正在连接数据库,但无法看到我的数据库。这就是为什么我添加了一个测试插入,以强制数据库出现在`show dbs`中。
admin 0.000GB
config 0.000GB
local 0.000GB
projectdb 0.000GB
然而,这没有帮助。
您可以在下面看到我的application.properties。
spring.data.mongodb.authentication-database=
spring.data.mongodb.database=projectdb
spring.data.mongodb.field-naming-strategy=
spring.data.mongodb.grid-fs-database=
spring.data.mongodb.host=172.25.1.5
spring.data.mongodb.password=
spring.data.mongodb.port=27017
spring.data.mongodb.username=
以下是我的RestController,我不知道它是否有用,但我还是添加了它。
...
我肯定可以连接到两个主机,我已经测试过这一点。**是否有人遇到过这样的问题,并可以建议如何解决?**
**编辑1**
在替换了这些行之后:
spring.data.mongodb.database=projectdb
spring.data.mongodb.host=172.25.1.5
spring.data.mongodb.port=27017
使用以下内容:
spring.data.mongodb.uri=mongodb://172.25.1.5:27017/projectdb
出现了以下错误(还有更多错误,但其他所有错误都是由这个错误引起的):
Caused by: java.lang.IllegalStateException: Invalid mongo configuration, either uri or host/port/credentials/replicaSet must be specified
at org.springframework.util.Assert.state(Assert.java:76) ~[spring-core-5.2.9.RELEASE.jar:5.2.9.RELEASE]
...
我还附上了我的pom.xml文件。
...
**编辑2**
此主题:https://stackoverflow.com/questions/38819112/spring-boot-setting-up-mongodb-repository,遗憾的是,不能帮助我。它提到添加数据库名称,但我已经像我在评论中写的那样添加了。还有其他原因导致这个错误。
Please note that I've only provided the translated content you provided without any additional information. If you have any specific questions or need further assistance, feel free to ask.
英文:
I am trying to create a website that will connect to the databases on the external host. For this purpose, I created two virtual machines. One contains the MongoDB database, and the other one contains the website created in spring-boot + react. I am currently at the stage of creating a RestController. I got this error when trying to send a test request:
2020-10-25 17:39:38.492 ERROR 25849 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.data.mongodb.UncategorizedMongoDbException: Exception authenticating MongoCredential{mechanism=SCRAM-SHA-1, userName='', source='', password=<hidden>, mechanismProperties=<hidden>}; nested exception is com.mongodb.MongoSecurityException: Exception authenticating MongoCredential{mechanism=SCRAM-SHA-1, userName='', source='', password=<hidden>, mechanismProperties=<hidden>}] with root cause
java.lang.IllegalArgumentException: state should be: databaseName is not empty
at com.mongodb.assertions.Assertions.isTrueArgument(Assertions.java:101) ~[mongodb-driver-core-4.0.5.jar:na]
at com.mongodb.MongoNamespace.checkDatabaseNameValidity(MongoNamespace.java:59) ~[mongodb-driver-core-4.0.5.jar:na]
at com.mongodb.MongoNamespace.<init>(MongoNamespace.java:108) ~[mongodb-driver-core-4.0.5.jar:na]
at com.mongodb.internal.connection.CommandHelper.getCommandMessage(CommandHelper.java:89) ~[mongodb-driver-core-4.0.5.jar:na]
at com.mongodb.internal.connection.CommandHelper.sendAndReceive(CommandHelper.java:83) ~[mongodb-driver-core-4.0.5.jar:na]
at com.mongodb.internal.connection.CommandHelper.executeCommand(CommandHelper.java:33) ~[mongodb-driver-core-4.0.5.jar:na]
at com.mongodb.internal.connection.SaslAuthenticator.sendSaslStart(SaslAuthenticator.java:158) ~[mongodb-driver-core-4.0.5.jar:na]
at com.mongodb.internal.connection.SaslAuthenticator.access$100(SaslAuthenticator.java:40) ~[mongodb-driver-core-4.0.5.jar:na]
at com.mongodb.internal.connection.SaslAuthenticator$1.run(SaslAuthenticator.java:54) ~[mongodb-driver-core-4.0.5.jar:na]
at com.mongodb.internal.connection.SaslAuthenticator$1.run(SaslAuthenticator.java:47) ~[mongodb-driver-core-4.0.5.jar:na]
at com.mongodb.internal.connection.SaslAuthenticator.doAsSubject(SaslAuthenticator.java:207) ~[mongodb-driver-core-4.0.5.jar:na]
at com.mongodb.internal.connection.SaslAuthenticator.authenticate(SaslAuthenticator.java:47) ~[mongodb-driver-core-4.0.5.jar:na]
at com.mongodb.internal.connection.InternalStreamConnectionInitializer.authenticate(InternalStreamConnectionInitializer.java:152) ~[mongodb-driver-core-4.0.5.jar:na]
at com.mongodb.internal.connection.InternalStreamConnectionInitializer.initialize(InternalStreamConnectionInitializer.java:61) ~[mongodb-driver-core-4.0.5.jar:na]
at com.mongodb.internal.connection.InternalStreamConnection.open(InternalStreamConnection.java:128) ~[mongodb-driver-core-4.0.5.jar:na]
at com.mongodb.internal.connection.UsageTrackingInternalConnection.open(UsageTrackingInternalConnection.java:50) ~[mongodb-driver-core-4.0.5.jar:na]
at com.mongodb.internal.connection.DefaultConnectionPool$PooledConnection.open(DefaultConnectionPool.java:435) ~[mongodb-driver-core-4.0.5.jar:na]
at com.mongodb.internal.connection.DefaultConnectionPool.get(DefaultConnectionPool.java:117) ~[mongodb-driver-core-4.0.5.jar:na]
at com.mongodb.internal.connection.DefaultConnectionPool.get(DefaultConnectionPool.java:102) ~[mongodb-driver-core-4.0.5.jar:na]
at com.mongodb.internal.connection.DefaultServer.getConnection(DefaultServer.java:90) ~[mongodb-driver-core-4.0.5.jar:na]
at com.mongodb.internal.binding.ClusterBinding$ClusterBindingConnectionSource.getConnection(ClusterBinding.java:119) ~[mongodb-driver-core-4.0.5.jar:na]
at com.mongodb.internal.operation.FindOperation$1.call(FindOperation.java:628) ~[mongodb-driver-core-4.0.5.jar:na]
at com.mongodb.internal.operation.FindOperation$1.call(FindOperation.java:625) ~[mongodb-driver-core-4.0.5.jar:na]
at com.mongodb.internal.operation.OperationHelper.withReadConnectionSource(OperationHelper.java:462) ~[mongodb-driver-core-4.0.5.jar:na]
at com.mongodb.internal.operation.FindOperation.execute(FindOperation.java:625) ~[mongodb-driver-core-4.0.5.jar:na]
at com.mongodb.internal.operation.FindOperation.execute(FindOperation.java:77) ~[mongodb-driver-core-4.0.5.jar:na]
at com.mongodb.client.internal.MongoClientDelegate$DelegateOperationExecutor.execute(MongoClientDelegate.java:190) ~[mongodb-driver-sync-4.0.5.jar:na]
at com.mongodb.client.internal.MongoIterableImpl.execute(MongoIterableImpl.java:135) ~[mongodb-driver-sync-4.0.5.jar:na]
at com.mongodb.client.internal.MongoIterableImpl.iterator(MongoIterableImpl.java:92) ~[mongodb-driver-sync-4.0.5.jar:na]
at org.springframework.data.mongodb.core.MongoTemplate.executeFindMultiInternal(MongoTemplate.java:2782) ~[spring-data-mongodb-3.0.4.RELEASE.jar:3.0.4.RELEASE]
at org.springframework.data.mongodb.core.MongoTemplate.doFind(MongoTemplate.java:2518) ~[spring-data-mongodb-3.0.4.RELEASE.jar:3.0.4.RELEASE]
at org.springframework.data.mongodb.core.MongoTemplate.doFind(MongoTemplate.java:2500) ~[spring-data-mongodb-3.0.4.RELEASE.jar:3.0.4.RELEASE]
at org.springframework.data.mongodb.core.MongoTemplate.find(MongoTemplate.java:856) ~[spring-data-mongodb-3.0.4.RELEASE.jar:3.0.4.RELEASE]
at org.springframework.data.mongodb.repository.support.SimpleMongoRepository.findAll(SimpleMongoRepository.java:383) ~[spring-data-mongodb-3.0.4.RELEASE.jar:3.0.4.RELEASE]
at org.springframework.data.mongodb.repository.support.SimpleMongoRepository.findAll(SimpleMongoRepository.java:205) ~[spring-data-mongodb-3.0.4.RELEASE.jar:3.0.4.RELEASE]
at org.springframework.data.mongodb.repository.support.SimpleMongoRepository.findAll(SimpleMongoRepository.java:55) ~[spring-data-mongodb-3.0.4.RELEASE.jar:3.0.4.RELEASE]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_265]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_265]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_265]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_265]
at org.springframework.data.repository.core.support.ImplementationInvocationMetadata.invoke(ImplementationInvocationMetadata.java:72) ~[spring-data-commons-2.3.4.RELEASE.jar:2.3.4.RELEASE]
at org.springframework.data.repository.core.support.RepositoryComposition$RepositoryFragments.invoke(RepositoryComposition.java:382) ~[spring-data-commons-2.3.4.RELEASE.jar:2.3.4.RELEASE]
at org.springframework.data.repository.core.support.RepositoryComposition.invoke(RepositoryComposition.java:205) ~[spring-data-commons-2.3.4.RELEASE.jar:2.3.4.RELEASE]
at org.springframework.data.repository.core.support.RepositoryFactorySupport$ImplementationMethodExecutionInterceptor.invoke(RepositoryFactorySupport.java:549) ~[spring-data-commons-2.3.4.RELEASE.jar:2.3.4.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.doInvoke(QueryExecutorMethodInterceptor.java:155) ~[spring-data-commons-2.3.4.RELEASE.jar:2.3.4.RELEASE]
at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.invoke(QueryExecutorMethodInterceptor.java:130) ~[spring-data-commons-2.3.4.RELEASE.jar:2.3.4.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:80) ~[spring-data-commons-2.3.4.RELEASE.jar:2.3.4.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:95) ~[spring-aop-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212) ~[spring-aop-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at com.sun.proxy.$Proxy65.findAll(Unknown Source) ~[na:na]
at com.project.videoSerwis.services.RoleService.getRoles(RoleService.java:28) ~[classes/:na]
at com.project.videoSerwis.controller.VideoServiceController.getRoles(VideoServiceController.java:93) ~[classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_265]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_265]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_265]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_265]
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190) ~[spring-web-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138) ~[spring-web-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:105) ~[spring-webmvc-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:878) ~[spring-webmvc-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:792) ~[spring-webmvc-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040) ~[spring-webmvc-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943) ~[spring-webmvc-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) ~[spring-webmvc-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898) ~[spring-webmvc-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:626) ~[tomcat-embed-core-9.0.38.jar:4.0.FR]
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883) ~[spring-webmvc-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:733) ~[tomcat-embed-core-9.0.38.jar:4.0.FR]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) ~[tomcat-embed-core-9.0.38.jar:9.0.38]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.38.jar:9.0.38]
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) ~[tomcat-embed-websocket-9.0.38.jar:9.0.38]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.38.jar:9.0.38]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.38.jar:9.0.38]
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100) ~[spring-web-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.38.jar:9.0.38]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.38.jar:9.0.38]
at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93) ~[spring-web-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.38.jar:9.0.38]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.38.jar:9.0.38]
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201) ~[spring-web-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.38.jar:9.0.38]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.38.jar:9.0.38]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202) ~[tomcat-embed-core-9.0.38.jar:9.0.38]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97) [tomcat-embed-core-9.0.38.jar:9.0.38]
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541) [tomcat-embed-core-9.0.38.jar:9.0.38]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:143) [tomcat-embed-core-9.0.38.jar:9.0.38]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) [tomcat-embed-core-9.0.38.jar:9.0.38]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78) [tomcat-embed-core-9.0.38.jar:9.0.38]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343) [tomcat-embed-core-9.0.38.jar:9.0.38]
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:374) [tomcat-embed-core-9.0.38.jar:9.0.38]
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) [tomcat-embed-core-9.0.38.jar:9.0.38]
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868) [tomcat-embed-core-9.0.38.jar:9.0.38]
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1590) [tomcat-embed-core-9.0.38.jar:9.0.38]
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-9.0.38.jar:9.0.38]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_265]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_265]
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-9.0.38.jar:9.0.38]
at java.lang.Thread.run(Thread.java:748) [na:1.8.0_265]
I test Request with swagger.
From what I understand, the application is connecting to the database but cannot see my database. That's why I put a test insert to force db to show up in the show dbs
.
admin 0.000GB
config 0.000GB
local 0.000GB
projectdb 0.000GB
However, it did not help.
You can see my application.properties below.
spring.data.mongodb.authentication-database=
spring.data.mongodb.database=projectdb
spring.data.mongodb.field-naming-strategy=
spring.data.mongodb.grid-fs-database=
spring.data.mongodb.host=172.25.1.5
spring.data.mongodb.password=
spring.data.mongodb.port=27017
spring.data.mongodb.username=
Below is my RestController, I don't know if it will be useful but still I add it.
@RestController
@CrossOrigin(origins = "http://localhost:3000")
public class VideoServiceController {
@Autowired
private IUserService iUserService;
@Autowired
private IRoleService iRoleService;
@RequestMapping(value = "/postUser", method = RequestMethod.POST)
@ResponseBody
public void postUser(@ModelAttribute UserPOJO userPOJO) {
iUserService.postUser(userPOJO);
}
@RequestMapping(value = "/getUsers", method = RequestMethod.GET)
@ResponseBody
public List<UserPOJO> getUsers() {
return iUserService.getUserPojo();
}
@RequestMapping(value = "/getUsersById", method = RequestMethod.GET)
@ResponseBody
public UserPOJO getUsersById(@RequestParam("Id") String id) {
return iUserService.getUserById(id);
}
@RequestMapping(value = "/getUserByEmailAndPass", method = RequestMethod.GET)
@ResponseBody
public UserPOJO getUserByEmailAndPass(@RequestParam("email") String email,
@RequestParam("password") String password) {
return iUserService.getUserByEmailAndPassword(email,password);
}
@RequestMapping(value = "/putPassword", method = RequestMethod.PUT)
@ResponseBody
public void putPassword(@RequestParam("id") String id,@RequestParam("newPass") String newPass,
@RequestParam("oldPass") String oldPass) {
iUserService.putPassword(id,newPass,oldPass);
}
@RequestMapping(value = "/deleteUser", method = RequestMethod.DELETE)
@ResponseBody
public Boolean deleteUser(@RequestParam("id") String id) {
return iUserService.deleteUser(id);
}
@RequestMapping(value = "/postRole", method = RequestMethod.POST)
@ResponseBody
public void postRole(@ModelAttribute RolePOJO role) {
iRoleService.postRole(role);
}
@RequestMapping(value = "/getRoles", method = RequestMethod.GET)
@ResponseBody
public List<RolePOJO> getRoles() {
return iRoleService.getRoles();
}
//5eca9ef4bf824a6544b732a5 USER
@RequestMapping(value = "/deleteRole", method = RequestMethod.DELETE)
@ResponseBody
public Boolean deleteRole(@RequestParam("id") String id) {
return iRoleService.deleteRole(id);
}
@RequestMapping(value = "/putUserRole", method = RequestMethod.PUT)
@ResponseBody
public void putUserRole(@RequestParam("userId") String userId, @RequestParam("roleName") String roleName) {
iUserService.putUserRole(userId, roleName);
}
}
I can definitely connect with both hosts, I have already tested this. Has anyone had such problems and could advise on how to fix it?
Edit 1
After replacing these lines:
spring.data.mongodb.database=projectdb
spring.data.mongodb.host=172.25.1.5
spring.data.mongodb.port=27017
With:
spring.data.mongodb.uri=mongodb://172.25.1.5:27017/projectdb
This error began to appear (There were more errors, but all the others were caused by this one):
Caused by: java.lang.IllegalStateException: Invalid mongo configuration, either uri or host/port/credentials/replicaSet must be specified
at org.springframework.util.Assert.state(Assert.java:76) ~[spring-core-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.boot.autoconfigure.mongo.MongoClientFactorySupport.validateConfiguration(MongoClientFactorySupport.java:79) ~[spring-boot-autoconfigure-2.3.4.RELEASE.jar:2.3.4.RELEASE]
at org.springframework.boot.autoconfigure.mongo.MongoClientFactorySupport.computeClientSettings(MongoClientFactorySupport.java:68) ~[spring-boot-autoconfigure-2.3.4.RELEASE.jar:2.3.4.RELEASE]
at org.springframework.boot.autoconfigure.mongo.MongoClientFactorySupport.createMongoClient(MongoClientFactorySupport.java:61) ~[spring-boot-autoconfigure-2.3.4.RELEASE.jar:2.3.4.RELEASE]
at org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration.mongo(MongoAutoConfiguration.java:57) ~[spring-boot-autoconfigure-2.3.4.RELEASE.jar:2.3.4.RELEASE]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_265]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_265]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_265]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_265]
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154) ~[spring-beans-5.2.9.RELEASE.jar:5.2.9.RELEASE]
... 85 common frames omitted
I am also adding my pom.xml file.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.project</groupId>
<artifactId>videoSerwis</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>videoSerwis</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.4.16.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.reactivex.rxjava2/rxjava -->
<dependency>
<groupId>io.reactivex.rxjava2</groupId>
<artifactId>rxjava</artifactId>
<version>2.2.19</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
Edit 2
This topic: https://stackoverflow.com/questions/38819112/spring-boot-setting-up-mongodb-repository unfortunately, it cannot help me. It mentions adding the name of the database but I already added it as I wrote in the commentary. Something else must be causing this error.
答案1
得分: 3
尝试使用文档中的以下配置类:
@Configuration
class ApplicationConfig extends AbstractMongoClientConfiguration {
@Value("${spring.data.mongodb.uri}")
public String mongoUri;
@Override
protected void configureClientSettings(MongoClientSettings.Builder builder) {
builder.applyConnectionString(new ConnectionString(mongoUri));
}
@Override
protected String getDatabaseName() {
return "projectdb";
}
}
英文:
Try using the following config class from the documentation:
@Configuration
class ApplicationConfig extends AbstractMongoClientConfiguration {
@Value("${spring.data.mongodb.uri}")
public String mongoUri;
@Override
protected void configureClientSettings(MongoClientSettings.Builder builder) {
builder.applyConnectionString(new ConnectionString(mongoUri));
}
@Override
protected String getDatabaseName() {
return "projectdb";
}
}
答案2
得分: 0
如果来自 SpringBoot 3.1.0... 这是一个已知错误并在此处报告。
解决方法是在URI中添加数据库名称,示例如下,对我起效了:
spring.data.mongodb.database=demo-db
spring.data.mongodb.uri=mongodb://localhost:27018/${spring.data.mongodb.database}?readPreference=primaryPreferred&directConnection=true
英文:
If coming from SpringBoot 3.1.0...It's a known error and reported here.
workaround is add the Db name in the uri itself.Example below. Worked for me
spring.data.mongodb.database=demo-db
spring.data.mongodb.uri=mongodb://localhost:27018/${spring.data.mongodb.database}?readPreference=primaryPreferred&directConnection=true
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论