【问题标题】:Request of Angular dont map my API in BackEnd (Spring Boot)Angular 的请求不要在后端(Spring Boot)中映射我的 API
【发布时间】:2019-11-08 12:02:59
【问题描述】:

我有一个错误,我不知道为什么,但是 angular 在 spring boot 中没有激活我的 api,我没有找到错误。

这是代码。

服务(角度)

setPost(postId:number):Observable<boolean>{
    return this.http.post<boolean>(this.myAppUrl+this.myApiPostUrl+postId+'/setPost',this.authService.getLoggedUserFromSessionStorage())
      .pipe(
        retry(1),
        catchError(this.errorHandler)
      );
  }

组件(角度)

isAlreadyOpen(){
    this.postService.isOpen(this.postId).subscribe( isAuth => {
      if(isAuth) {
        this.cantModify();
      } else {
        console.log('im here');
        this.postService.setPost(this.postId);
        this.router.navigate(['/post/edit/', this.postId]);
      }
  });
  }

代码在else中输入没有问题,因为chrome控制台打印'im here'。

控制器 (JAVA)

@RequestMapping(value = "/posts/{id}/setPost", method = RequestMethod.POST)
    public ResponseEntity<Boolean> setPost(@PathVariable("id") int idPost, @RequestBody UserDTO userCorrente) {
        PostUser postUser = postUserService.set(idPost, userCorrente);
        if (postUser != null) {
            return new ResponseEntity<>(true, HttpStatus.OK);
        } else {
            return new ResponseEntity<>(false, HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

角度 this.postService.isOpen(这个api,完美运行)

不工作this.postService.setPost,我试图删除所有地图,看看是否像那样

isAlreadyOpen(){
        this.postService.setPost(this.postId);
        this.router.navigate(['/post/edit/', this.postId]);
      }
  }

但是什么都没有,不起作用,我在 Chrome 的 DEV TOOLS(网络)中看不到请求,在 Spring Console 中也看不到任何内容。

【问题讨论】:

    标签: java angular spring api spring-boot


    【解决方案1】:

    你需要在this.postService.setPost(this.postId)上调用subscribe(),否则不会触发请求。

    【讨论】:

    • 非常感谢,我没注意到。你拯救了我的一天。
    猜你喜欢
    • 2019-11-25
    • 1970-01-01
    • 2019-05-12
    • 1970-01-01
    • 1970-01-01
    • 2022-01-12
    • 1970-01-01
    • 2018-03-18
    • 2020-09-12
    相关资源
    最近更新 更多