1

Hello I want to send a string from my Angular 4 post request to my java spring mvc controller and return it's value.

in angular 4 function:

let body = 'example'

http
  .post('favourite', body)
  .subscribe(
      data => {
        console.log('favourite received');
      },
      error => {
        console.log('an error occured');
      }
    )

in my java code:

@RequestMapping(value= "/favourite", method = RequestMethod.POST)
@ResponseBody
public void createFavourite(@RequestParam(value="body") String favourite){
    Favourite.setFav(favourite);
}

Essentially I want to send just thestring: 'example' as the body and then receive it, again as a string in my java spring controller and set it for my Favourite fav value, so then if I have a Favourites.getFav() function in Favourite it will return "example". What am I doing wrong with the requestsand how can I make it work?

JJey
  • 147
  • 1
  • 3
  • 14
  • I think you're confusing post "body" with url params... have a look at this Angular example https://stackoverflow.com/questions/35212341/angular2-http-post-request-parameters and then see how what you send might be interpreted by your java controller https://stackoverflow.com/questions/13715811/requestparam-vs-pathvariable. There are different ways to do this but possibly what you want is something like /favourite?body=something which you're close to but not quite there yet :) – Aidan Moriarty Oct 22 '17 at 18:54
  • So to make it work I should do something like: let body='favourite=example' and then in @RequestParam(value = "favourite", required = false) String fav) { FavouritesetFav(fav); } Am I going the right direction? – JJey Oct 22 '17 at 20:21

1 Answers1

-1

First of all you have to inject http, or httpClient. Second thing, why you have only 'favourite' in your post method? It is wrong. First param of every http method is full path like: http://localhost:8080/favourite

Here you have a lot of examples: https://angular.io/guide/http

Adam A
  • 157
  • 1
  • 18
  • Your answer has a lot of questions in it ;) Why not just add comments to clarify the original question instead? – Aidan Moriarty Oct 22 '17 at 18:55
  • Yeaa, a lot of questions but in my opinion really helpful ;) – Adam A Oct 22 '17 at 19:01
  • So to make it work I should do something like: let body='favourite=example' and then in @RequestParam(value = "favourite", required = false) String fav) { FavouritesetFav(fav); } Am I going the right direction? – JJey Oct 22 '17 at 20:20
  • I can't tell what to do in spring, but as I mentioned your angular code is incorrect. – Adam A Oct 22 '17 at 20:22
  • Then how is the correct way to write it.. I want to send a string to the backend - that's all. – JJey Oct 23 '17 at 06:30
  • did you read https://stackoverflow.com/questions/35212341/angular2-http-post-request-parameters at all, you can see there how a key-value pair is built and sent to the backend, and then your @RequestParam on the backend which is named for the same "key" will capture the incoming "value" from your Angular request – Aidan Moriarty Oct 23 '17 at 08:43