Angular 12前端返回编码的get请求



我的数据库使用mysql,后端使用spring。我的前端在正确路由时抛出了这个奇怪的错误:点击此处查看

正如您所看到的,末尾的路径是%7Bid%7D(从后端看类似于{id}(
http错误代码总是3个错误代码中的一个:400400500
后端看起来不错,我只真正得到过这个错误代码:

2022-02-04 15:30:31.465  WARN 15200 --- [nio-8081-exec-7] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.method.annotation.MethodArgumentTypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'java.lang.Long'; nested exception is java.lang.NumberFormatException: For input string: "{id}"] 


这是有问题的控制器(获取请求(:

@CrossOrigin
@RestController
@RequestMapping(path = "/api/patient")
public class PatientPortalController {
@Autowired
private PatientPortalRepo patientPortalRepo;
@PostMapping("/patientportal")
public PatientPortal createPatientPortal(@RequestBody PatientPortal patientportal) {
return patientPortalRepo.save(patientportal);
}


@GetMapping("/patientportal/{id}")
public ResponseEntity<PatientPortal> getpatientPortal(@PathVariable Long id){
PatientPortal patientportal = patientPortalRepo.findByPatientPortalId(id);
if(patientportal.getId()>0 && patientportal!=null)
return new ResponseEntity<PatientPortal>(patientportal, HttpStatus.OK);
return new ResponseEntity<PatientPortal>(patientportal, HttpStatus.BAD_REQUEST);
}}

值得注意的是,我曾尝试过后端
尝试过将响应实体更改为类型为long并返回id,多次尝试过重构控制器;尝试过更改装饰器/路径周围,20x检查了类型是否正确检查是否有id以外的任何类型正在抛出它查看我是否实现了任何拒绝访问的安全性是否检查添加onetoone是否会使它在前端弹出。它在后端运行良好(返回一个列表,我认为它是患者门户对象(,但我要么路由不正确,要么缺少一些安全性;要么存在一些类型错误、存在一些逻辑错误认为但是问题出在前端
这是我调用前端方法的代码硬编码要测试的值

this.patientloginservice.loginPatient(this.patient).subscribe(data=>(this.route.navigate(['api/patient/patientportal/1'])),error=>console.log('error'));


这里是代码的服务位置

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http'
import { Observable } from 'rxjs'
import { PatientPortal } from './patientportal';
@Injectable({
providedIn: 'root'
})
export class PatientService {
private baseURL = "http://localhost:8081/api/patient/patientportal";
constructor(private httpClient: HttpClient) { }
getPatientPortalList(): Observable<PatientPortal[]> {
return this.httpClient.get<PatientPortal[]>(`${this.baseURL}`);
}
createPatientPortal(patientportal: PatientPortal): Observable<Object>{
return this.httpClient.post<Object>(`${this.baseURL}`, patientportal);
}
getPatientPortalById(id: number): Observable<PatientPortal>{
return this.httpClient.get<PatientPortal>(`${this.baseURL}/{id}`);
}
updatePatientPortal(id: number, patientportal: PatientPortal): Observable<Object>{
return this.httpClient.put(`${this.baseURL}/{id}`, patientportal);
}
deletePatientPortal(id: number): Observable<Object>{
return this.httpClient.delete(`${this.baseURL}/{id}`);
}
}

任何帮助都将不胜感激,谢谢。同样,正如我所说的,路由尽我所能正确路由,但渲染的表没有填充数据,它会抛出错误。我正在使用登录,即重定向/路由患者的详细信息。

您使用的Template文字不正确。

不只是{id},它应该是${id},就像您对${this.baseUrl}所做的那样

希望这能解决你的问题。

最新更新