简体   繁体   English

错误不显示数据库中的数据(spring boot + angular 8)

[英]Error doesn't show data from database (spring boot + angular 8)

I want to display data on frontend from database using RestController but it doesnt show nothing and no errors so I need a help to resolve this issue any suggestion may help me and thanks我想使用 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: class:

export class Employe {

    id: number;
    name: string;
    prenom: string;
}

AppComponent.html:应用组件.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();

}

Go to network tab and see if there is any backend call. Go 到网络选项卡,看看是否有任何后端调用。 If there is no backend call, you need to debug your service file on why http call is not getting through.如果没有后端调用,你需要调试你的服务文件为什么 http 调用没有通过。 If the backend call is happening, there can be 2 possibilities.如果正在发生后端调用,则可能有 2 种可能性。 Either there is an error with status code anything other than 200. This will be comparatively easier to fix once we know what the error is.要么是 200 以外的状态码有错误。一旦我们知道错误是什么,这将相对更容易修复。 Or if response status code is 200 then you are getting the data from backend.或者,如果响应状态代码是 200,那么您正在从后端获取数据。 I see you have an interface.我看到你有一个接口。 Why do you have an interface if you are not implementing it anywhere?如果您没有在任何地方实现它,为什么还要有一个接口?

I add this to the main and the issue is fixed我将此添加到主要内容中,问题已解决

@SpringBootApplication(exclude= {
    org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration.class
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM