错误未显示数据库中的数据(Spring Boot + angular 8)

问题描述 投票:0回答:2

我想使用RestController在数据库的前端显示数据,但是它什么也不显示,也没有错误,所以我需要帮助解决此问题,任何建议都可能对我有帮助,谢谢

ServiceClass:

@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}`);
    }

ComponentService:

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();
    }

ICollaborateur:

public interface ICollaborateur {


    List<Collaborateur> getEmp();

}
angular api spring-boot spring-restcontroller
2个回答
0
投票

转到网络标签,查看是否有任何后端呼叫。如果没有后端调用,则需要调试服务文件以了解为什么HTTP调用无法通过。如果正在进行后端调用,则可能有两种可能性。可能是状态码不是200的任何错误。一旦我们知道了什么是错误,这将相对容易解决。或者,如果响应状态代码为200,那么您正在从后端获取数据。我看到你有一个界面。如果没有在任何地方实现,为什么会有接口?


0
投票

我将此添加到主要内容,问题已解决@SpringBootApplication(exclude = {org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration.class})

© www.soinside.com 2019 - 2024. All rights reserved.