我是 Sping Boot、rest api 和 angular12 的新手,
我在 vscode 中运行我的程序来调用返回的 api,我收到错误“加载资源失败:服务器响应状态为 404(未找到)”
我的代码:后端控制器:
@RestController
@RequestMapping("/evaluationController/")
public class EvaluationController {
@Autowired
private EvaluationRepository evaluationrepository;
//get all evaluationsnotes
@CrossOrigin(origins = "http://localhost:4200/")
@GetMapping("/notes")
public List<EvaluationModel> getAllEvaluations(){
return evaluationrepository.findAll();
}
//post notes
@PostMapping("/notes")
public EvaluationModel createEvaluationNote(@RequestBody EvaluationModel evaluationNote) {
return evaluationrepository.save(evaluationNote);
}
}
我的前端 angular12 服务
@Injectable({
providedIn: 'root'
})
export class EvaluationserviceService {
private baseUrl!: "http://localhost:8080/evaluationController/notes";
constructor(private httpClient: HttpClient) { }
httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json'
})
}
getEvaluationNotes():Observable<Evaluationotes[]>{
return this.httpClient.get<Evaluationotes[]>(`${this.baseUrl}`);
}
}
我的打字稿文件
@Component({
selector: 'app-fin-evaluation',
templateUrl: './fin-evaluation.component.html',
styleUrls: ['./fin-evaluation.component.css']
})
export class FinEvaluationComponent implements OnInit {
evaluationNote!: Evaluationotes[];
constructor(private evaluationNoteService: EvaluationserviceService ) { }
ngOnInit(): void {
this.getAllNotes();
}
private getAllNotes(){
this.evaluationNoteService.getEvaluationNotes().subscribe(data=>{
this.evaluationNote = data;
});
}
}
谢谢!