Search code examples
javaspringspring-data-jparestful-url

@Repository and @RestController together


I have a working JpaRepository as an interface, and a working Restcontroller.

They are two types currently and I like to join them to one!

This is what i have:

@Repository
@RestController("problem")
public interface ProblemsController extends JpaRepository<Problem, Integer> {

  @RequestMapping(value = "all", method = RequestMethod.GET)
  List<Problem> findAll();
}

But when i call http://localhost:8080/application/problem/all I get a 404.

What to do?


Solution

  • You could use this way..but am not get what exactly your are expecting.

    import java.util.List;
    
    import org.springframework.data.repository.PagingAndSortingRepository;
    import org.springframework.data.repository.query.Param;
    import org.springframework.data.rest.core.annotation.RepositoryRestResource;
    
    @RepositoryRestResource(collectionResourceRel = "people", path = "people")
    public interface PersonRepository extends PagingAndSortingRepository<Person, Long> {
    
        List<Person> findByLastName(@Param("name") String name);
    
    }
    

    This is act as a Repositry and REST web service.

    if you got an error. You can check UI page is prperly locating this interface or Not