Java Spring工具套件(3.6.3.SR1)启动程序项目无法连接到数据库
我的spring starter项目有一个非常奇怪的问题: 我使用的是STS,它的版本是3.6.3.SR1。我还安装了mongodb,它的版本是2.6.7。 我想使用STS连接到mongodb。因此,我尝试了教程中的项目: 文件->新建->导入Spring入门内容。然后选择“访问数据Mongodb”。然后我运行gs访问数据mongodb complete,它工作得非常好。教程在此处[在此处输入链接说明][1] 然而,当我创建SpringStarter项目时,我选择了MongoDB,Rest存储库。我从上面复制了完全相同的代码,但它不起作用 有人能帮我吗 Pom.xml http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0Java Spring工具套件(3.6.3.SR1)启动程序项目无法连接到数据库,java,spring,mongodb,Java,Spring,Mongodb,我的spring starter项目有一个非常奇怪的问题: 我使用的是STS,它的版本是3.6.3.SR1。我还安装了mongodb,它的版本是2.6.7。 我想使用STS连接到mongodb。因此,我尝试了教程中的项目: 文件->新建->导入Spring入门内容。然后选择“访问数据Mongodb”。然后我运行gs访问数据mongodb complete,它工作得非常好。教程在此处[在此处输入链接说明][1] 然而,当我创建SpringStarter项目时,我选择了MongoDB,Rest存储库
<groupId>org.test</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<start-class>demo.DemoApplication</start-class>
<java.version>1.7</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-data-rest</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
}
导入java.util.List
导入org.springframework.data.mongodb.repository.MongoRepository
公共接口CustomerRepository扩展了MongoRepository{
public Customer findByFirstName(String firstName);
public List<Customer> findByLastName(String lastName);
}连接到MongodB端口时出现连接被拒绝异常,我会尝试杀死MongodB并重新启动它,看看这是否解决了问题,但下面是我在异常详细信息中看到的,{com.MongodB.MongoException$Network:exception opening the socket},这是由{java.net.ConnectException:connection拒绝}引起的}]我使用的是默认端口27017,STS也使用默认端口27017,我使用终端连接到它,它工作得很好。奇怪的是,当我使用STS starter项目时,它不工作:(。我想我的问题是如何使用spring starter项目连接到Mongodb,因为所有的“导入spring入门内容"我有一个连接到MongodB的项目,但我使用命令行中的gradle进行编译,我刚刚在STS中创建了一个项目,并使用相同的代码连接到MongodB,没有问题,发布您用于连接MongodB的代码,如果您使用gradle或pom.xml,如果您使用maven,我可能会发布您的gradle.buildB我们能够检测到问题,我只是更新它。这真的很奇怪
public Customer findByFirstName(String firstName);
public List<Customer> findByLastName(String lastName);
@Autowired
private CustomerRepository repository;
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
repository.deleteAll();
// save a couple of customers
repository.save(new Customer("Alice", "Smith"));
repository.save(new Customer("Bob", "Smith"));
// fetch all customers
System.out.println("Customers found with findAll():");
System.out.println("-------------------------------");
for (Customer customer : repository.findAll()) {
System.out.println(customer);
}
System.out.println();
// fetch an individual customer
System.out.println("Customer found with findByFirstName('Alice'):");
System.out.println("--------------------------------");
System.out.println(repository.findByFirstName("Alice"));
System.out.println("Customers found with findByLastName('Smith'):");
System.out.println("--------------------------------");
for (Customer customer : repository.findByLastName("Smith")) {
System.out.println(customer);
}
}