com.mongodb.MongoSocketReadException: Prematurely达到end of stream........如何解决
Posted
技术标签:
【中文标题】com.mongodb.MongoSocketReadException: Prematurely达到end of stream........如何解决【英文标题】:com.mongodb.MongoSocketReadException: Prematurely reached end of stream..............How to solve 【发布时间】:2019-03-10 11:28:11 【问题描述】:对不起,我的代码很长。 但我只是一个初学者。我想用 MongoDB 学习 Spring Framework。
我收到此异常,但我已阅读有关此问题的一些帖子。但是,我不明白以及如何解决它。
我还没有创建应用程序配置。这是导致异常的原因吗? 谁能帮我解决这个问题? 我真的很感激。非常感谢....
package com.mywebapp.dao;
import java.util.Date;
import java.util.List;
import org.bson.types.ObjectId;
import org.springframework.data.mongodb.repository.ReactiveMongoRepository;
import org.springframework.stereotype.Repository;
import com.mywebapp.model.Location;
import com.mywebapp.model.User;
@Repository
public interface UserRepository extends ReactiveMongoRepository<User, String>
List<User> findAllUsers();
User findBy_id(ObjectId id);
User findByUsername(String username);
List<User> findByFirstName(String firstName);
List<User> findByLastName(String lastName);
User findByEmail(String email);
List<User> findByDateOfBirth(Date dob);
List<User> findByLocation(Location location);
package com.mywebapp.controller;
import java.util.Date;
import java.util.List;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.mywebapp.model.Location;
import com.mywebapp.model.User;
import com.mywebapp.service.UserService;
@RestController
@RequestMapping("/user")
public class UserController
@Autowired
private UserService userService;
@RequestMapping(value="/",method = RequestMethod.GET)
public List<User> getAllUser()
return userService.findAllUsers();
@RequestMapping(value="/username",method = RequestMethod.GET)
public User getUserByUserName(@PathVariable("username") String userName)
return userService.findByUserName(userName);
@RequestMapping(value="/firstname",method = RequestMethod.GET)
public List<User> getUserByFirstName(@PathVariable("firstname") String firstName)
return userService.findByFirstName(firstName);
@RequestMapping(value="/lastname",method = RequestMethod.GET)
public List<User> getUserByLastName(@PathVariable("lastname") String lastName)
return userService.findByLastName(lastName);
@RequestMapping(value="/email",method = RequestMethod.GET)
public User getUserByEmail(@PathVariable("email") String email)
return userService.findByEmail(email);
@SuppressWarnings("deprecation")
@RequestMapping(value="/dob",method = RequestMethod.GET)
public List<User> getUserByDOB(@PathVariable("dob") String dob) throws Exception
Date dateOfBirth = new Date(dob);
return userService.findByDateOfBirth(dateOfBirth);
@RequestMapping(value="/location",method = RequestMethod.GET)
public List<User> getUserByLocation(@PathVariable("location") Location location)
return userService.findByLocation(location);
@RequestMapping(value= "/id", method = RequestMethod.GET)
public User getUserById(@PathVariable("id") ObjectId id)
return userService.findById(id);
package com.mywebapp.service;
import java.util.Date;
import java.util.List;
import org.bson.types.ObjectId;
import org.springframework.stereotype.Service;
import com.mywebapp.dao.UserRepository;
import com.mywebapp.model.Location;
import com.mywebapp.model.User;
@Service
public class UserService
private UserRepository userRepo;
public UserService()
public List<User> findAllUsers()
return userRepo.findAllUsers();
public User findById(ObjectId id)
return userRepo.findBy_id(id);
public User findByUserName(String username)
return userRepo.findByUsername(username);
public List<User> findByFirstName(String firstName)
return userRepo.findByFirstName(firstName);
public List<User> findByLastName(String lastName)
return userRepo.findByLastName(lastName);
public User findByEmail(String email)
return userRepo.findByEmail(email);
public List<User> findByDateOfBirth(Date dob)
return userRepo.findByDateOfBirth(dob);
public List<User> findByLocation(Location location)
return userRepo.findByLocation(location);
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>MyWebApplication</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>MyWebApplication</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>10</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>2.12.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
【问题讨论】:
欢迎来到 Stack Overflow!请参观并通读帮助中心,特别是如何提问。你最好的选择是做你的研究,搜索关于 SO 的相关主题,然后试一试。在进行更多研究和搜索后,发布一个最小的、完整的和可验证的尝试示例,并具体说明您遇到的问题,这可以帮助您获得更好的答案! 【参考方案1】:在资源文件夹下的application.properties
中定义以下属性,替换您的实际值。如果默认安装了 MongoDB 并且没有用户名/密码,那么您不需要定义用户名和密码属性。
spring.data.mongodb.host=localhost
spring.data.mongodb.port=27017
spring.data.mongodb.username=*********
spring.data.mongodb.password=*********
spring.data.mongodb.database=demo
更新
更新存储库以扩展 MongoRepository
@Repository
public interface UserRepository extends MongoRepository<User, ObjectId>
从您的存储库中删除 List<User> findAllUsers();
。
将服务中的所有方法更改为以下
public List<User> findAllUsers()
return userRepo.findAll();
【讨论】:
请告诉我您的文档类:com.mywebapp.model.User;以及您在启动应用程序或调用任何特定方法时看到的错误?? @TyMinh 检查我更新的答案并确保您的用户文档中有一些记录..以上是关于com.mongodb.MongoSocketReadException: Prematurely达到end of stream........如何解决的主要内容,如果未能解决你的问题,请参考以下文章