我一直在尝试从运行在我电脑上的spa获取一个数组请求localhost:8080,它必须是正确的端口,因为它在控制台中被告知,如果我尝试请求其他端口,它( Postman )会说请求失败。我很难找到404的原因,因为我没有可以处理的错误。
休息控制器:
@RestController
public class Controller {
private AtomicInteger rCounter;
private AtomicInteger pCounter;
private List<Registration> registrationList = new ArrayList<Registration>();
private List<Project> projectList = new ArrayList<Project>();
@PostConstruct
public void init() {
this.rCounter = new AtomicInteger();
this.pCounter = new AtomicInteger();
}
// THIS SECTION IS DEDICATED TO THE REGISTRATION REQUESTS
// get all Registrations
@GetMapping("/registrations")
public List<Registration> getAllRegistrations() {
return this.registrationList;
}
// get a single Registration
@GetMapping("/registrations/{id}")
public Registration getRegistration(@PathVariable("id") int id) {
for (Registration reg : registrationList) {
if (reg.getId() == id) {
return reg;
}
}
throw new ResponseStatusException(HttpStatus.NOT_FOUND,
String.format("Registration with ID %s not found!", id));
}
// create a Registration
@PostMapping("/createRegistration")
@ResponseStatus(HttpStatus.CREATED)
public void createRegistration(@Valid @RequestBody Registration requestBody) {
Registration reg = new Registration(rCounter.getAndIncrement(), requestBody.getStudents(),
requestBody.getPreferredProjects());
this.registrationList.add(reg);
}
// update a Registration
@PutMapping("/updateRegistration/{id}")
public void updateRegistration(@PathVariable("id") int id, @Valid @RequestBody Registration requestBody) {
requestBody.setId(id);
for (Registration reg : registrationList) {
if (reg.getId() == id) {
this.registrationList.set(id, requestBody);
}
}
throw new ResponseStatusException(HttpStatus.NOT_FOUND,
String.format("Registration with ID %s not found!", id));
}
// delete a Registration
@DeleteMapping("/deleteRegistration/{id}")
public void deleteRegistration(@PathVariable("id") int id) {
for (Registration reg : registrationList) {
if (reg.getId() == id) {
this.registrationList.remove(id);
}
}
throw new ResponseStatusException(HttpStatus.NOT_FOUND,
String.format("Registration with ID %s not found!", id));
}
// THIS SECTION IS DEDICATED TO THE PROJECT REQUESTS
// get all Projects
@GetMapping("/projects")
public List<Project> getAllProjects() {
return this.projectList;
}
// get a single Project
@GetMapping("/projects/{id}")
public Project getProject(@PathVariable("id") int id) {
for (Project project : projectList) {
if (project.getId() == id) {
return project;
}
}
throw new ResponseStatusException(HttpStatus.NOT_FOUND, String.format("Project with ID %s not found!", id));
}
// create a Project
@PostMapping("/createProject/{id}")
@ResponseStatus(HttpStatus.CREATED)
public void createProject(@Valid @RequestBody Project requestBody) {
Project project = new Project(pCounter.getAndIncrement(), requestBody.getName());
this.projectList.add(project);
}
// update a Project
@PutMapping("/updateProject/{id}")
public void updateProject(@PathVariable("id") int id, @Valid @RequestBody Project requestBody) {
requestBody.setId(id);
for (Project project : projectList) {
if (project.getId() == id) {
this.projectList.set(id, requestBody);
}
}
throw new ResponseStatusException(HttpStatus.NOT_FOUND, String.format("Project with ID %s not found!", id));
}
// delete a Project
@DeleteMapping("/deleteProject/{id}")
public void deleteProject(@PathVariable("id") int id) {
for (Project project : projectList) {
if (project.getId() == id) {
this.projectList.remove(id);
}
}
throw new ResponseStatusException(HttpStatus.NOT_FOUND, String.format("Project with ID %s not found!", id));
}
}
api bean:
// enable cross-origin resource sharing (CORS)
@Bean
public WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurer() {
@Override
public void addCorsMappings(CorsRegistry registry) {
// allow CORS requests for all resources and HTTP methods from the frontend
// origin
registry.addMapping("/**").allowedMethods("OPTIONS", "HEAD", "GET", "PUT", "POST", "DELETE")
.allowedOrigins("http://localhost:8080");
}
};
}
如果你需要看更多的只是写一个评论,我会通过编辑提供它。谢谢你的帮助!
暂无答案!
目前还没有任何答案,快来回答吧!