简体   繁体   中英

Override default save Entity on Spring Data Rest

I would like to override the default CrudRepository save method that is also exported to Rest api:

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


    @Override
    @RestResource(exported=false)
    User save(User user);


}

In my ApiController I have set up a requestmapping like this:

    @RequestMapping(value = "/", produces = "application/json", method = RequestMethod.POST)
        @ResponseBody
        public ResponseEntity<Resource<User>> registerUser(
          @RequestParam("name") String name, 
          @RequestParam("alias") String alias, 
          @RequestParam("email") String email, 
          @RequestParam("password") String password,
          @RequestParam("dateOfBirth") String dateOfBirth,
          @RequestParam("imageIdentifier") String imageIdentifier) {

         User user = new User();
         //try {
         // userReposiotry.save(user);
         //} catch (Exception e) {

         //}
         Resource<User> resource = toResource(user);
         return new ResponseEntity<Resource<User>>(resource, HttpStatus.OK);
}

The problem is when I try to POST to localhost:8080/api/users it returns a "Method Not allowed" which is good because it was set "exported=false" But how can I implement my own POST for localhost:8080/api/users ? Thanks

Another way to do it is to create a custom repository implementation like so:

@RepositoryRestResource(path = "users")
    public interface UserRepository extends JpaRepository<User, Long>, UserRepositoryCustom {

    @Override
    @RestResource(exported=false)
    User save(User user);
}

public interface UserRepositoryCustom {
    <S extends User> S save(T entity);
}

public UserRepositoryImpl implements UserRepositoryCustom {
    <S extends User> S save(T entity) {
        // implementation code...
    }
}

If you look at the CrudRepository you will find a method <S extends T> S save(S entity); , that's where I got the save(..) from, just changed the extends T to extends User .

The other thing that I would pay attention to is the naming of the classes/interfaces, try to be consistent. The way I named them should work for you, the UserRepositoryImpl must have that name in order for this to work.

Doing this you won't have to set exported=false and you can just use the save() method as you would do normal.

Found a solution:

@BasePathAwareController
@RequestMapping("/users")
public class RestApiController implements ResourceProcessor<Resource<User>>{
    @Autowired
    private EntityLinks entityLinks;

    @RequestMapping(method=RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<Resource<User>> saveUser(@Param("name") String name) {
        // Testing
        System.out.println(name);
        Resource<User> resource = new Resource<>(new User());
        return new ResponseEntity<>(resource , HttpStatus.OK);
    }

    @Override
    public Resource<User> process(Resource<User> resource) {
            LinkBuilder lb = entityLinks.linkFor(User.class);
            resource.add(new Link(lb.toString()));
        return resource;
    }
}

The CrudRepository save is still set as exported=false as in my question.

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