1

By following the official tutorial for projections in spring data mongodb https://docs.spring.io/spring-data/mongodb/docs/current/reference/html/#projections will get an

java.lang.IllegalArgumentException: Couldn't find PersistentEntity for type class com.sun.proxy.$Proxy109!

for the NamesOnly Projection:

interface NamesOnly {

  String getFirstname();
  String getLastname();
}

@RepositoryRestResource
interface PersonRepository extends Repository<Person, UUID> {

  Collection<NamesOnly> findByLastname(@Param("lastName") String lastname);
}

Can one get this example to work?

1 Answers1

1

You need to define a @RestController class and call the findByLastname repository method from the controller, like:

@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;
 }
}
Federico Gatti
  • 535
  • 1
  • 9
  • 22
  • No, the the findByLastname is exposed through @RepositoryRestResource. Either way i"m able to call it, but as stated i get the java.lang.IllegalArgumentException: Couldn't find PersistentEntity for type class com.sun.proxy.$Proxy109! error – FlorianFusseder Jun 06 '18 at 07:22
  • I also try to use **@RepositoryRestResource** but Spring gives me the same error. I think that is not possible use directly the REST API that Spring creates with this types of projection. Note that in the documentation they **never** refer to use the **@RepositoryRestResource**. If you want to use the projection directly using the REST API created by Spring I suggest you the **fields** field inside the **@Query** annotation see [https://stackoverflow.com/questions/32108953/how-to-return-only-specific-fields-for-a-query-in-spring-data-mongodb] – Federico Gatti Jun 06 '18 at 08:10