ng new my-app
cd my-app
ng serve
@RestController
@RequestMapping("/api")
public class MyController {
@GetMapping("/data")
public String getData() {
return "Hello from Java!";
}
}
typescript
import { Component, OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
data: string;
constructor(private http: HttpClient) {}
ngOnInit() {
this.http.get('/api/data').subscribe((response: any) => {
this.data = response;
});
}
}
html
<h1>{{ data }}</h1>