Javascript 从角度传递时,spring boot无法识别路径变量数据
我使用了angular 7,我将angular服务等级的值传递为:Javascript 从角度传递时,spring boot无法识别路径变量数据,javascript,java,angular,spring,typescript,Javascript,Java,Angular,Spring,Typescript,我使用了angular 7,我将angular服务等级的值传递为: executeHelloWorldBeanServiceWithPathVariable(name){ console.log("name coming from here"+name); return this.httpClient.get<HelloWorldBean>('http://localhost:8080/hello-world/path-variable/${name}');
executeHelloWorldBeanServiceWithPathVariable(name){
console.log("name coming from here"+name);
return this.httpClient.get<HelloWorldBean>('http://localhost:8080/hello-world/path-variable/${name}');
console.log("hello world bean service executed");
}
它在控制台中打印,因为这里没有问题
在我的弹簧靴中,我声明:
@GetMapping(path="/hello-world/path-variable/{name}")
public HelloWorldBean helloWorldBeanPathVariable(@PathVariable("name") String name) {
System.out.print("name is"+name);
return new HelloWorldBean(String.format("Hello world %s", name));
}
当我尝试使用以下方法进行调试时,从angular传递的name参数未打印:
System.out.print("name is"+name);
但它是在el表达式中显示的
因此,在我的UI中,我得到:
您似乎没有在
executeHelloWorldBeanServiceWithPathVariable
方法上使用实际语法。我假设您试图使用它是因为${}
表达式
这可能是请求未正确解析的原因。您应该使用反引号(`),而不是单引号或双引号
executeHelloWorldBeanServiceWithPathVariable(name){
return this.httpClient.get<HelloWorldBean>(`http://localhost:8080/hello-world/path-variable/${name}`);
}
在处理typescript时,使用backticks`而不是使用'或'。
特别是当你计算一些值时,比如“我的字符串”+${myname}@ashwinkarki很乐意帮忙!这不是问题的答案,更像是一个评论。
executeHelloWorldBeanServiceWithPathVariable(name){
return this.httpClient.get<HelloWorldBean>(`http://localhost:8080/hello-world/path-variable/${name}`);
}
constructor(
private yourService: YourService,
) { }
ngOnInit() {
this.yourService.executeHelloWorldBeanServiceWithPathVariable('someName').subscribe(res => {
console.log(res);
// do the rest here
})
}