Reputation: 127
I have a rest api set up at api/books, and one can send a new book object there, and it will be added to the database. The guestion is, how can I correctly catch what is being POST'ed, so one can for example, validate what is being sent?
@RequestMapping(value="/api/books", method = RequestMethod.POST)
public String bookSavePost(@RequestBody Book book) {
bookRepository.save(book);
return "redirect:/api/books";
}
This works, as in it saves the book and I can catch what the user sends, but with this enabled, it overrides the default REST method, which doesn't allow one to actually view the api anymore. If I change this particular method to GET, it returns a string "redirect:/api/books", so it doesn't even redirect anything. Is there some way to actually redirect it to the rest api endpoint?
Upvotes: 1
Views: 92
Reputation: 2211
You can write your own reuquest Interceptor .
Spring provides HandlerInterceptor class :
Here is a quick sample how to do this: https://www.baeldung.com/spring-mvc-handlerinterceptor
Upvotes: 3
Reputation: 503
A redirect requires three two things to be successful: A HTTP code of 301 or 302 AND a location
header that includes the endpoint to which you want the client to visit.
E.g., in the headers section you should have
location: '/correct/endpoint'
Upvotes: 0