My Spring Boot controller sends String "hello Angular" as a simple response to http request.
@Controller
@RequestMapping(path = "/u")
public class UController {
@RequestMapping("/greet")
public @ResponseBody String greet() {
System.out.println("you are in the method greet() ");
return "hello Angular";
}
}
I see at console (System.out) that Angular has requested right method and the response should have been sent to client.
I would like to print this message by Angular on the web site, so I subscribe the Observable from http client, but it does not appear on the screen.
Here is my Angular component:
import { Component, OnInit } from '@angular/core';
import { HttpClientModule } from '@angular/common/http';
import { HttpClient, HttpHeaders } from '@angular/common/http';
@Component({
selector: 'app-root',
template: `
<div style="text-align:center">
<h1> {{greeting}} </h1>
</div>
<router-outlet></router-outlet>
`,
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit{
greeting : string;
constructor(private http: HttpClient){ }
ngOnInit() {
this.http.get<string>('http://localhost:8080/u/greet').subscribe(data => { this.greeting = data; });
}
}
I really have no idea how can I fix it.
console.log(data)inside the body ofsubscribe(), what gets logged? Also, do you see the request execute in the network tab of your browser's developer tools? - Alexander Staroselsky