3

Hi I am trying to develop REST api using Spring boot + Spring Data JPA + Spring Data REST

I want to expose only writable part of my User ( basically no GET or GET ALL ) entity which is as below

@Entity(name = "User")
public class User implements Serializable {

    @Id
    @GeneratedValue(strategy= GenerationType.AUTO)
    int id;

    @Column
    String login;

    @Column
    String password;

    @Column
    String username;

    @Column
    String address1;

    @Column
    String address2;

    @Column
    String city;

    @Column
    String state;

    @Column
    String zip;

    @Column
    String country;

    @Column
    String creditcard;


}

and datarepository is as below:

@RepositoryRestResource(path = "users")
public interface UserRepository extends CrudRepository<User, String> {

}

How can I achieve this?

Pramod S. Nikam
  • 4,271
  • 4
  • 38
  • 62
  • Please refer link https://stackoverflow.com/questions/29169717/how-to-prevent-some-http-methods-from-being-exported-from-my-mongorepository – Vinod Bokare Sep 14 '17 at 11:21

1 Answers1

5

You can override and mark methods with the @RestResource(exported = false).

The methods are

T findOne(ID id);         // /users/<ID>
Iterable<T> findAll();    // /users
Iterable<T> findAll(Iterable<ID> ids);

You will be getting the 405 Method Not Allowed HTTP status for all GET requests to the repository.


Hint: It is not necessary to mark fields with the @Column to make them reflected as database columns.

Andrew Tobilko
  • 48,120
  • 14
  • 91
  • 142