简体   繁体   中英

returning all result using spring-data-rest

I am following spring data rest from https://spring.io/guides/gs/accessing-data-rest/ and I am only using

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-rest</artifactId>
        </dependency> 

I would like to know how can I return all records ( without pagination ) but not using spring-boot-starter-web.I wants to keep my code as small as possible.

I tried following but it is not working

@RepositoryRestResource(collectionResourceRel = "people" , path = "people")
public interface PersonRepository  extends PagingAndSortingRepository<Person, Long> {
    List<Person> findAllByLastName(@Param("name") String name);
    default List<Person> findAll(){
        Pageable pageable = null;
        return (List<Person>) this.findAll(pageable);
    };
}

I mean if I have whole MVC, I can do it but I like to keep my code to minimum.

Spring Data REST is itself a Spring MVC application and is designed in such a way that it should integrate with your existing Spring MVC applications with little effort. An existing (or future) layer of services can run alongside Spring Data REST with only minor additional work.


If you are using current version of spring boot, there is no need to mark your repository with @RepositoryRestResource ; also spring will auto-configure Spring Data Rest once it found the spring-data-rest dependency in your path, bellow you will find steps with minimum config :

In pom.xml :

        <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-jpa</artifactId>
        </dependency>

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
        </dependency>

Define your Entity + Repository :

Order.java

@Entity(name = "SampleOrder")
@Data
public class Order {

    @Id @GeneratedValue//
    private Long id;
    private String name;
}

OrderRepository.java

public interface OrderRepository extends CrudRepository<Order, Long> {

}

Application.java

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

Test your API :

curl http://localhost:8080
< HTTP/1.1 200 OK
< Content-Type: application/hal+json
{ "_links" : {
    "orders" : {
      "href" : "http://localhost:8080/orders"
    }
  }
}

As @abdelghani-roussi shows, you can use the CrudRepository instead of the PagingAndSortingRepository , eg:

public interface PersonRepository extends CrudRepository<Person, Long> {
  List<Person> findAllByLastName(@Param("name") String name);
  // don't need to define findAll(), it's defined by CrudRepository
}

and then the default findAll() method will return a List<Person> that isn't paged.

Note: as I mentioned in my comment, by including the dependency on spring-boot-starter-data-rest you are also pulling in the Web dependencies, so you can't avoid that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM