【问题标题】:Path variable data is not recognized by spring boot when passed from angular从角度传递时,Spring Boot 无法识别路径变量数据
【发布时间】:2019-07-26 01:54:01
【问题描述】:

我使用了 angualr 7,并且我将来自 angular 服务类的值传递为:

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");
   }

在我测试时,该名称正在控制台中打印:

console.log("name coming from here"+name);

在这里打印在控制台没有问题。

在我的春季靴子中,我声明为:

@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 传递的名称参数没有被打印出来:

System.out.print("name is"+name);

但它显示在el表达式中

所以在我的 UI 中,我得到了:

【问题讨论】:

    标签: javascript java angular spring typescript


    【解决方案1】:

    您的executeHelloWorldBeanServiceWithPathVariable 方法似乎没有使用实际的template literal 语法。我假设您因为 ${} 表达式而尝试使用它。

    这可能是请求未正确解析的原因。您应该使用反引号(`)而不是单引号或双引号。

    executeHelloWorldBeanServiceWithPathVariable(name){
      return this.httpClient.get<HelloWorldBean>(`http://localhost:8080/hello-world/path-variable/${name}`);
    }
    

    此外,要从 API 请求中实际返回 observable,您必须在需要它的组件上向它发送 subscribe()

    例如,

    constructor(
      private yourService: YourService,
    ) { }
    
    ngOnInit() {
      this.yourService.executeHelloWorldBeanServiceWithPathVariable('someName').subscribe(res => {
        console.log(res);
        // do the rest here
      })
    }
    

    【讨论】:

    • @ashwinkarki 很高兴为您提供帮助!
    【解决方案2】:

    在打字稿上工作时,使用反引号 ` 而不是使用 ' 或 " 进行连接。 特别是在你评估一些值的地方,比如 "my string"+${myname}

    【讨论】:

    • 这不是问题的答案 - 更像是评论。
    猜你喜欢
    • 2016-02-21
    • 2016-08-11
    • 1970-01-01
    • 2017-05-17
    • 2011-05-20
    • 2015-01-13
    • 2023-04-07
    • 1970-01-01
    • 2021-09-07
    相关资源
    最近更新 更多