1

I have a simple MongoRepository I would like to modify to return the generated ObjectId on post(save()).

public interface EmployeeRepository extends MongoRepository<Employee, String>
{   
    public void delete(Employee employee);
    public Employee save(Employee employee);
    public Employee findOne(String id);
    public List<Employee> findAll();
    public Employee findByName(String principal);
}

I have explored ways to generate the id client side and pass it in the post BUT I really want Spring to handle this.

I've tried intercepting with a controller and returning the object in the ResponseBody, like so:

@RequestMapping(value=URI_EMPLOYEES, method=RequestMethod.POST)
public @ResponseBody Employee addEmployee(@RequestBody Employee employee) {
    return repo.save(employee);
}

Problem with this is it forces me to re-work all the HATEOAS related logic Spring handles for me. Which is a MAJOR pain. (Unless I'm missing something.)

What's the most effective way of doing this without having to replace all of the methods?

Nestor Ledon
  • 1,925
  • 2
  • 23
  • 36

2 Answers2

1

Was using @Controller instead of @RepositoryRestController which was causing things to act up.

We can now easily override the POST method on this resource to return whatever we want while keeping spring-data-rest's implementation of the EmployeeRepository intact.

@RepositoryRestController
public class EmployeeController {

    private final static String URI_EMPLOYEES = "/employees";

    @Autowired private EmployeeRepository repo;

    @RequestMapping(value=URI_EMPLOYEES, method=RequestMethod.POST)
    public @ResponseBody HttpEntity<Employee> addVideo(@RequestBody Employee employee) {
        return new ResponseEntity<Employee>(repo.save(employee), HttpStatus.OK);
    }
}
Nestor Ledon
  • 1,925
  • 2
  • 23
  • 36
  • 1
    This doesn't work for me. Changing from `@RestController` to `@RepositoryRestController` didn't help. – oxyt Apr 25 '16 at 10:06
-1

Have a look at the interface:

public Employee save(Employee employee)

You can get the saved entity by simply doing

Employee saved = repository.save(employee);
return saved.getId();

The being said, you surely can generate the Id and set it via setId(). But once an ID is saved, it is immutable. Changing the Id and saving that entity would result in a new document saved in MongoDB.

Markus W Mahlberg
  • 19,711
  • 6
  • 65
  • 89
  • 1
    I feel this is given. The problem is I can't simply override the functionality without losing the HATEOAS and HAL formatting Spring provides. – Nestor Ledon Oct 17 '14 at 13:20
  • 1
    I guess I don't get it. You want HATEOAS, but for a given object. That should be easily done by letting the controller method return a HttpEntity and creating an according ResponseEntity like in `return new ResponseEntity(repo.save(employee), HttpStatus.OK);` Am I missing something here? – Markus W Mahlberg Oct 17 '14 at 13:55
  • 1
    You're on point. My real concern was that when I implemented a Controller it would hide the Repository, forcring me to set re-implement everything. But it was because I was using (at)Controller instead of (at)RepositoryRestController. Now I can over JUST the post method, and leave the rest to Spring. Thanks and sorry for not being clear. – Nestor Ledon Oct 17 '14 at 14:11