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.

2
  • 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<Movie> createMovie(@RequestBody Movie movie), there are other factory methods in ResponseEntity for different Http status types. ResponseEntity.ok will return status code 200 Commented Dec 3, 2017 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. Commented Dec 3, 2017 at 21:36

1 Answer 1

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);
Sign up to request clarification or add additional context in comments.

3 Comments

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 > stackoverflow.com/questions/32905917/… :)
@PeterBoomsma sorry and yeah I omitted getter/setter for brevity.
though json serializer can access private fields

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.