【发布时间】:2021-06-25 09:14:16
【问题描述】:
这是我的 Employee 类
public class Employee {
@Id
private String id;
private String firstName;
private String lastName;
private float salary;
private List<Address> addresses;
private Date joiningDate;
我想从我的 mongoDB 中获取特定员工的地址列表。 而且我知道要返回一个特定的字段
query.fields().include(addresses);
但这对我不起作用。我收到 500 内部服务器错误。
Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.data.mapping.model.MappingInstantiationException: Failed to instantiate com.example.demo.model.Employee using constructor public com.example.demo.model.Employee(java.lang.String,java.lang.String,java.lang.String,float,java.util.List,java.util.Date) with arguments 60d5798c319336085ac8130a,hello,null,null,null,null] with root cause
这是我的代码
员工资料库
@Repository
public class EmployeeRepository {
@Autowired
MongoTemplate mongoTemplate;
public List<Employee> findAddressesByFirstName(String firstName) {
Query query = new Query(Criteria.where("firstName").is(firstName));
query.fields().include("addresses");
return mongoTemplate.find(query, Employee.class);
}
}
员工服务
public List<Employee> findAddressesByFirstName(String firstName) {
return employeeRepository.findAddressesByFirstName(firstName);
}
员工控制器
@GetMapping("/firstName/{firstName}")
public List<Employee> getAddressesByFirstName(@PathVariable("firstName") String firstName){
return employeeService.findAddressesByFirstName(firstName);
}
【问题讨论】:
-
你有来自日志的堆栈跟踪吗?