错误不显示来自数据库的数据 (spring boot + angular 8)
Error doesn't show data from database (spring boot + angular 8)
我想使用 RestController 在前端显示来自数据库的数据,但它什么也没有显示,也没有错误,所以我需要帮助来解决这个问题,任何建议都可能对我有帮助,谢谢
服务类:
@Injectable({
providedIn: 'root'
})
export class EmployeService{
private URL_RESOURCES:String = 'http://localhost:8080/api/employe';
constructor(private http: HttpClient){
}
public getEmp():Observable<Employe[]>{
return this.http.get<Employe[]>(`${this.URL_RESOURCES}`);
}
组件服务:
export class EmployeesComponent implements OnInit {
_employesArray:Employe[]=[];
constructor(private http:HttpClient, private employeservice:EmployeService) {
}
ngOnInit() {
this.dataSource.sort = this.sort;
this.dataSource.paginator = this.paginator;
this.reloadData();
}
reloadData() {
this.employeservice.getEmp().subscribe(
data=>{
this._employesArray=data;
},
err=>{
alert("An error has occured")
}
);
}
}
class:
export class Employe {
id: number;
name: string;
prenom: string;
}
AppComponent.html:
<tbody>
<tr *ngFor="let employee of _employesArray ">
<td>{{employee.id}}</td>
<td>{{employee.name}}</td>
<td>{{employee.prenom}}</td>
</tr>
</tbody>
CollaborateurController:
@RestController
@CrossOrigin(origins="http://localhost:4200")
@RequestMapping("/api")
public class CollaborateurController {
@Autowired
private CollaborateurRepository collaborateurRepository;
@GetMapping(value="/employe")
public List<Collaborateur> getEmp() {
return (List<Collaborateur>) collaborateurRepository.findAll();
}
我合作者:
public interface ICollaborateur {
List<Collaborateur> getEmp();
}
转到网络选项卡,查看是否有任何后端调用。如果没有后端调用,您需要调试您的服务文件,了解为什么 http 调用无法通过。
如果发生后端调用,则可能有 2 种可能性。状态码不是 200 的任何错误。一旦我们知道错误是什么,这将相对容易修复。或者,如果响应状态代码为 200,那么您正在从后端获取数据。
我看到你有一个接口。如果你没有在任何地方实现它,为什么你有一个接口?
我把这个添加到主要部分,问题就解决了
@SpringBootApplication(exclude= {
org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration.class
})
我想使用 RestController 在前端显示来自数据库的数据,但它什么也没有显示,也没有错误,所以我需要帮助来解决这个问题,任何建议都可能对我有帮助,谢谢
服务类:
@Injectable({
providedIn: 'root'
})
export class EmployeService{
private URL_RESOURCES:String = 'http://localhost:8080/api/employe';
constructor(private http: HttpClient){
}
public getEmp():Observable<Employe[]>{
return this.http.get<Employe[]>(`${this.URL_RESOURCES}`);
}
组件服务:
export class EmployeesComponent implements OnInit {
_employesArray:Employe[]=[];
constructor(private http:HttpClient, private employeservice:EmployeService) {
}
ngOnInit() {
this.dataSource.sort = this.sort;
this.dataSource.paginator = this.paginator;
this.reloadData();
}
reloadData() {
this.employeservice.getEmp().subscribe(
data=>{
this._employesArray=data;
},
err=>{
alert("An error has occured")
}
);
}
}
class:
export class Employe {
id: number;
name: string;
prenom: string;
}
AppComponent.html:
<tbody>
<tr *ngFor="let employee of _employesArray ">
<td>{{employee.id}}</td>
<td>{{employee.name}}</td>
<td>{{employee.prenom}}</td>
</tr>
</tbody>
CollaborateurController:
@RestController
@CrossOrigin(origins="http://localhost:4200")
@RequestMapping("/api")
public class CollaborateurController {
@Autowired
private CollaborateurRepository collaborateurRepository;
@GetMapping(value="/employe")
public List<Collaborateur> getEmp() {
return (List<Collaborateur>) collaborateurRepository.findAll();
}
我合作者:
public interface ICollaborateur {
List<Collaborateur> getEmp();
}
转到网络选项卡,查看是否有任何后端调用。如果没有后端调用,您需要调试您的服务文件,了解为什么 http 调用无法通过。 如果发生后端调用,则可能有 2 种可能性。状态码不是 200 的任何错误。一旦我们知道错误是什么,这将相对容易修复。或者,如果响应状态代码为 200,那么您正在从后端获取数据。 我看到你有一个接口。如果你没有在任何地方实现它,为什么你有一个接口?
我把这个添加到主要部分,问题就解决了
@SpringBootApplication(exclude= {
org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration.class
})