0

In my app users can add movie titles to their watchlist. In my component I have this function:

createMovie(movie: Movie): void {
  this._dataService.createMovie<Movie>({'name': 'Star Wars', 'id': 111, 'description': 'A war in the Stars'})
  .subscribe((data) => this.movies.push(data),
  error => () => {
      'something went wrong';
  },
  () => {
      // console.log(this.movies);
  });
}

This has some dummy info for now.

In my service I have:

public createMovie<T>(movie: Movie): Observable<T> {
    return this.http.post<T>('/api/movies/', movie, {headers: this.getToken()});
}

So I pass the movie object and the token to the back-end.

In my MovieController.java I have:

@RestController
@RequestMapping("api/movies")
public class MovieController {

  @Autowired
  private MovieService movieService;

  @RequestMapping(value = "/", method = RequestMethod.POST)
  public Movie createMovie(@RequestBody Movie movie){
      return movieService.createMovie(movie);
  }
}

And the createMovie function in the movieService:

@Override 
public Movie createMovie(Movie movie) {

    movieRepository.save(movie);

    User current_user = userService.getUser();
    current_user.addMovie(movie);
    userRepository.save(current_user);

    return movie;
}

This all works fine, but I would like to return a message to the angular application when a movie was successfully added to the list (database). I think I should use @ResponseBody for it, but I'm unsure how to return the movie object and a status text to the angular application.

So for example, when a movie is added I would like to return a message "movie.name was successfully added to your watchlist" from the back-end.

Peter Boomsma
  • 8,851
  • 16
  • 93
  • 185
  • If you are asking about http status code, then you can use factory `ResponseEntity.ok(movieService.createMovie(movie))` and your method return type would will be `public ResponseEntity createMovie(@RequestBody Movie movie)`, there are other factory methods in `ResponseEntity` for different Http status types. `ResponseEntity.ok` will return status code 200 – Zeronex Dec 03 '17 at 21:29
  • Hm I don't think that's what I'm thinking off. Maybe I should have said response body instead of response status. How can I return a response body with a message `Movie.name was added to your watchlist`. – Peter Boomsma Dec 03 '17 at 21:36

1 Answers1

2

To return a message alongside your object you can define a new class like

public class RestResponse<T>{

    private String message;
    private T obj;

    public RestResponse(String message, T obj){
    this.message = message;
    this.obj = obj;
    }

}

then in your Rest controller you can do

Movie result =  movieService.createMovie(movie);

return new RestResponse<Movie>(String.format("%s was added to your watchlist", movie.name ), result);
Zeronex
  • 434
  • 1
  • 3
  • 8
  • Thanks for pointing me in the right direction :). One tip, you should add getters/setters. I got an error and this question helped me out > https://stackoverflow.com/questions/32905917/how-to-return-json-data-from-spring-controller-using-responsebody :) – Peter Boomsma Dec 03 '17 at 22:06
  • @PeterBoomsma sorry and yeah I omitted getter/setter for brevity. – Zeronex Dec 03 '17 at 22:18
  • though json serializer can access private fields – Zeronex Dec 03 '17 at 22:19