通过遵循春季数据预测的官方教程,mongodb https://docs.spring.io/spring-data/mongodb/docs/current/reference/html/#projections将获得
java.lang.IllegalArgumentException:找不到针对的PersistentEntity
输入com.sun.proxy。$ Proxy109类!
仅用于名称投影:
interface NamesOnly {
String getFirstname();
String getLastname();
}
@RepositoryRestResource
interface PersonRepository extends Repository<Person, UUID> {
Collection<NamesOnly> findByLastname(@Param("lastName") String lastname);
}
可以让这个例子奏效吗?
最佳答案
您需要定义一个@RestController类,并从控制器中调用findByLastname存储库方法,例如:
@RestController
@RequestMapping("/api")
public class PersonController {
@Autowired
private PersonRepository personRepository;
@GetMapping(path = "/persons/findByLastname")
public Collection<NamesOnly> findByLastname(@Param("lastName") final String lastName) {
Collection<NamesOnly> result = personRepository.findByLastname(lastName);
return result;
}
}