在尝试从Spring应用程序发送数据到远程服务器上的MongoDB时出现错误。

huangapple go评论79阅读模式
英文:

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=&#39;&#39;, source=&#39;&#39;, password=&lt;hidden&gt;, mechanismProperties=&lt;hidden&gt;}; nested exception is com.mongodb.MongoSecurityException: Exception authenticating MongoCredential{mechanism=SCRAM-SHA-1, userName=&#39;&#39;, source=&#39;&#39;, password=&lt;hidden&gt;, mechanismProperties=&lt;hidden&gt;}] 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.&lt;init&gt;(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 = &quot;http://localhost:3000&quot;)
public class VideoServiceController {
@Autowired
private IUserService iUserService;
@Autowired
private IRoleService iRoleService;
@RequestMapping(value = &quot;/postUser&quot;, method = RequestMethod.POST)
@ResponseBody
public void postUser(@ModelAttribute UserPOJO userPOJO) {
iUserService.postUser(userPOJO);
}
@RequestMapping(value = &quot;/getUsers&quot;, method = RequestMethod.GET)
@ResponseBody
public List&lt;UserPOJO&gt; getUsers() {
return iUserService.getUserPojo();
}
@RequestMapping(value = &quot;/getUsersById&quot;, method = RequestMethod.GET)
@ResponseBody
public UserPOJO getUsersById(@RequestParam(&quot;Id&quot;) String id) {
return iUserService.getUserById(id);
}
@RequestMapping(value = &quot;/getUserByEmailAndPass&quot;, method = RequestMethod.GET)
@ResponseBody
public UserPOJO getUserByEmailAndPass(@RequestParam(&quot;email&quot;) String email,
@RequestParam(&quot;password&quot;) String password) {
return iUserService.getUserByEmailAndPassword(email,password);
}
@RequestMapping(value = &quot;/putPassword&quot;, method = RequestMethod.PUT)
@ResponseBody
public void putPassword(@RequestParam(&quot;id&quot;) String id,@RequestParam(&quot;newPass&quot;) String newPass,
@RequestParam(&quot;oldPass&quot;) String oldPass) {
iUserService.putPassword(id,newPass,oldPass);
}
@RequestMapping(value = &quot;/deleteUser&quot;, method = RequestMethod.DELETE)
@ResponseBody
public Boolean deleteUser(@RequestParam(&quot;id&quot;) String id) {
return iUserService.deleteUser(id);
}
@RequestMapping(value = &quot;/postRole&quot;, method = RequestMethod.POST)
@ResponseBody
public void postRole(@ModelAttribute RolePOJO role) {
iRoleService.postRole(role);
}
@RequestMapping(value = &quot;/getRoles&quot;, method = RequestMethod.GET)
@ResponseBody
public List&lt;RolePOJO&gt; getRoles() {
return iRoleService.getRoles();
}
//5eca9ef4bf824a6544b732a5 USER
@RequestMapping(value = &quot;/deleteRole&quot;, method = RequestMethod.DELETE)
@ResponseBody
public Boolean deleteRole(@RequestParam(&quot;id&quot;) String id) {
return iRoleService.deleteRole(id);
}
@RequestMapping(value = &quot;/putUserRole&quot;, method = RequestMethod.PUT)
@ResponseBody
public void putUserRole(@RequestParam(&quot;userId&quot;) String userId, @RequestParam(&quot;roleName&quot;) 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.

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
&lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
&lt;parent&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
&lt;version&gt;2.3.4.RELEASE&lt;/version&gt;
&lt;relativePath/&gt; &lt;!-- lookup parent from repository --&gt;
&lt;/parent&gt;
&lt;groupId&gt;com.project&lt;/groupId&gt;
&lt;artifactId&gt;videoSerwis&lt;/artifactId&gt;
&lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
&lt;name&gt;videoSerwis&lt;/name&gt;
&lt;description&gt;Demo project for Spring Boot&lt;/description&gt;
&lt;properties&gt;
&lt;java.version&gt;11&lt;/java.version&gt;
&lt;/properties&gt;
&lt;dependencies&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-data-mongodb&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.projectlombok&lt;/groupId&gt;
&lt;artifactId&gt;lombok&lt;/artifactId&gt;
&lt;optional&gt;true&lt;/optional&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
&lt;scope&gt;test&lt;/scope&gt;
&lt;exclusions&gt;
&lt;exclusion&gt;
&lt;groupId&gt;org.junit.vintage&lt;/groupId&gt;
&lt;artifactId&gt;junit-vintage-engine&lt;/artifactId&gt;
&lt;/exclusion&gt;
&lt;/exclusions&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;io.springfox&lt;/groupId&gt;
&lt;artifactId&gt;springfox-swagger2&lt;/artifactId&gt;
&lt;version&gt;2.9.2&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;io.springfox&lt;/groupId&gt;
&lt;artifactId&gt;springfox-swagger-ui&lt;/artifactId&gt;
&lt;version&gt;2.9.2&lt;/version&gt;
&lt;/dependency&gt;
&lt;!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core --&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.hibernate&lt;/groupId&gt;
&lt;artifactId&gt;hibernate-core&lt;/artifactId&gt;
&lt;version&gt;5.4.16.Final&lt;/version&gt;
&lt;/dependency&gt;
&lt;!-- https://mvnrepository.com/artifact/io.reactivex.rxjava2/rxjava --&gt;
&lt;dependency&gt;
&lt;groupId&gt;io.reactivex.rxjava2&lt;/groupId&gt;
&lt;artifactId&gt;rxjava&lt;/artifactId&gt;
&lt;version&gt;2.2.19&lt;/version&gt;
&lt;/dependency&gt;
&lt;/dependencies&gt;
&lt;build&gt;
&lt;plugins&gt;
&lt;plugin&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
&lt;/plugin&gt;
&lt;plugin&gt;
&lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
&lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
&lt;version&gt;3.1&lt;/version&gt;
&lt;configuration&gt;
&lt;source&gt;1.8&lt;/source&gt;
&lt;target&gt;1.8&lt;/target&gt;
&lt;encoding&gt;UTF-8&lt;/encoding&gt;
&lt;/configuration&gt;
&lt;/plugin&gt;
&lt;/plugins&gt;
&lt;/build&gt;
&lt;/project&gt;

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(&quot;${spring.data.mongodb.uri}&quot;)
public String mongoUri;
@Override
protected void configureClientSettings(MongoClientSettings.Builder builder) {
builder.applyConnectionString(new ConnectionString(mongoUri));
}
@Override
protected String getDatabaseName() {
return &quot;projectdb&quot;;
}
}

答案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&amp;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&amp;directConnection=true

huangapple
  • 本文由 发表于 2020年10月26日 01:00:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/64526433.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定