我只是补充道:
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
</dependency>
我的maven春装项目。我也有这个测试:
/*
* Copyright 2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package hello;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class GreetingControllerTests {
@Autowired
private MockMvc mockMvc;
@Test
public void noParamGreetingShouldReturnDefaultMessage() throws Exception {
this.mockMvc.perform(get("/greeting")).andDo(print()).andExpect(status().isOk())
.andExpect(jsonPath("$.content").value("Hello, World!"));
}
@Test
public void paramGreetingShouldReturnTailoredMessage() throws Exception {
this.mockMvc.perform(get("/greeting").param("name", "Spring Community"))
.andDo(print()).andExpect(status().isOk())
.andExpect(jsonPath("$.content").value("Hello, Spring Community!"));
}
}
出于某种原因,当我使用mvn clean package
spring boot构建我的项目时,现在尝试连接到mongodb:
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.1.7.RELEASE)
2019-10-11 20:36:58.418 INFO 14870 --- [ main] hello.GreetingControllerTests : Starting GreetingControllerTests on user-ThinkPad-X390 with PID 14870 (started by user in /home/user/repos/frontend-backend/backend)
2019-10-11 20:36:58.423 INFO 14870 --- [ main] hello.GreetingControllerTests : No active profile set, falling back to default profiles: default
2019-10-11 20:36:59.311 INFO 14870 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2019-10-11 20:36:59.565 INFO 14870 --- [ main] org.mongodb.driver.cluster : Cluster created with settings {hosts=[localhost:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500}
2019-10-11 20:36:59.591 INFO 14870 --- [localhost:27017] org.mongodb.driver.cluster : Exception in monitor thread while connecting to server localhost:27017
com.mongodb.MongoSocketOpenException: Exception opening socket
at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:67) ~[mongo-java-driver-3.8.2.jar:na]
at com.mongodb.internal.connection.InternalStreamConnection.open(InternalStreamConnection.java:126) ~[mongo-java-driver-3.8.2.jar:na]
at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:117) ~[mongo-java-driver-3.8.2.jar:na]
at java.base/java.lang.Thread.run(Thread.java:834) ~[na:na]
Caused by: java.net.ConnectException: Connection refused (Connection refused)
at java.base/java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:na]
at java.base/java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:399) ~[na:na]
如果我只是删除测试或mongo依赖项,上面的mongo连接就不会启动。
但是在上面的测试中没有提到mongo,那么spring为什么要尝试启动一个mongo连接呢?
如果我必须用一句话来回答,那是因为Springboot是固执己见的。一旦它通过自动配置
类注意到pom中的mongo依赖性,它就会尝试连接到mongo。
如果您想覆盖默认行为并告诉Springboot不要进行MongoutoConfiguration,那么您可以这样做
@SpringBootApplication(exclude=MongoAutoConfiguration.class)
public class YourMainApplication {
public static void main(String[] args) {
SpringApplication.run(TestApplication.class, args);
}
}
或者,您可以使用属性文件中的这一行执行此操作
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration
如果您执行上述任一操作,那么它将从应用程序中排除MongoutoConfiguration(不仅仅是从测试中)。这意味着当您启动应用程序时,您无法访问mongo(如果这是您想要的)。
由于<code>SpringbootTest</code>注释加载整个应用程序上下文,因此它查找这个主应用程序类。如果排除了一些自动配置,则它将排除,即使在测试中也是如此。所以你不会有连接到mongo的问题。
如果您希望只在测试中排除这种自动配置(这样在运行您的应用程序时不会有任何变化),您可以这样做
@TestPropertySource(properties=
{"spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration"})
@SpringBootTest
public class GreetingControllerTests {...}
错误:无法初始化主类com.companyname.bank.App,原因是:java.lang.NoClassDefFoundError:org/apache/http/client/ResponseHandler 我在pom.xml文件中添加了依赖项,在/src/lib中也添加了相关的.jar文件之后,这个报告一直出现。真的很困惑,不知道怎么解决。 请帮我一把。谢谢。 以下是我的操作流程: >
我们用Maven和Tycho构建了一个Eclipse插件。然而,目前我们仍然通过一堆手动添加的JAR文件而不是Maven来提供所有的项目依赖。这是由于以下原因:(1)依赖项不能通过标准的Eclipse更新站点获得(至少不能在当前版本中获得),(2)依赖项不能作为捆绑包获得。 这些依赖项中最大的部分是Selenium库(API,远程,特定于浏览器的库及其传递依赖项,例如Guava等)。 我浪费了几个
如何将此WorldEdit依赖项添加到Maven项目中?http://maven.sk89q.com/artifactory/repo/com/sk89q/worldedit/worldedit-bukkit/我需要6.1.1快照。 是否有算法来获取组ID工件ID和版本?
我正在尝试将protobuf依赖项添加到我的maven项目中。我已经使用mvn install安装了protobuf jar文件:install-file-Dpackaging = jar \-DgeneratePom = true \ < br >-DgroupId = com . Google . proto buf \-DartifactId = proto buf-Java \-Dfile
问题内容: 如何获取我拥有的jar文件并将其添加到Maven 2的依赖系统中?我将成为此依赖项的维护者,并且我的代码需要在类路径中使用此jar,以便对其进行编译。 问题答案: 您必须分两步执行此操作: 1.给您的JAR一个groupId,artifactId和版本,然后将其添加到您的存储库中。 如果您没有内部存储库,而只是试图将JAR添加到本地存储库,则可以使用任意groupId / artifa