Angular 12 前端返回编码的get请求
Angular 12 front end returning encoded get request
我正在为我的数据库使用 mysql,为我的后端使用 spring,angular 我的前端。我的前端在正确路由时抛出这个奇怪的错误:click here to see it
如你所见,最后的路径是%7Bid%7D
(后端看起来像{id}
)
http 错误代码 始终是 3 个之一:400、400 或 500
后端 看起来不错,我只真正收到过这个 错误代码:
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,已多次尝试重构控制器,已尝试更改decorators/paths around, 20x 检查类型是否正确, 检查是否有除 id 以外的任何类型正在抛出它, 检查我是否实施了任何拒绝访问的安全措施,检查添加 onetoone 是否会使其在前端弹出。它 在后端工作正常 (returns 列表 我假设是 patientportal 对象) 但是 我要么路由不正确,我缺少一些安全措施,一些类型错误、或存在一些逻辑错误。 我认为然而问题出在前端。
这是我调用前端方法的代码硬编码一个值来测试:
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}`);
}
}
任何帮助将不胜感激,谢谢。再次就像我说的那样,据我所知,路由是正确的,但是呈现的 table 没有填充数据,它会抛出该错误。我使用登录名 redirect/route 到 患者详细信息.
您错误地使用了模板文字。
而不是 {id}
它应该是 ${id}
就像你对 ${this.baseUrl}
所做的一样
希望能解决您的问题。
我正在为我的数据库使用 mysql,为我的后端使用 spring,angular 我的前端。我的前端在正确路由时抛出这个奇怪的错误:click here to see it
如你所见,最后的路径是%7Bid%7D
(后端看起来像{id}
)
http 错误代码 始终是 3 个之一:400、400 或 500
后端 看起来不错,我只真正收到过这个 错误代码:
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,已多次尝试重构控制器,已尝试更改decorators/paths around, 20x 检查类型是否正确, 检查是否有除 id 以外的任何类型正在抛出它, 检查我是否实施了任何拒绝访问的安全措施,检查添加 onetoone 是否会使其在前端弹出。它 在后端工作正常 (returns 列表 我假设是 patientportal 对象) 但是 我要么路由不正确,我缺少一些安全措施,一些类型错误、或存在一些逻辑错误。 我认为然而问题出在前端。
这是我调用前端方法的代码硬编码一个值来测试:
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}`);
}
}
任何帮助将不胜感激,谢谢。再次就像我说的那样,据我所知,路由是正确的,但是呈现的 table 没有填充数据,它会抛出该错误。我使用登录名 redirect/route 到 患者详细信息.
您错误地使用了模板文字。
而不是 {id}
它应该是 ${id}
就像你对 ${this.baseUrl}
希望能解决您的问题。