java - Angular 的请求不要在后端映射我的 API(Spring Boot)

标签 java angular spring api spring-boot

我有一个错误,我不知道为什么,但是 Angular 没有在 Spring Boot 中激活我的 api,而且我没有找到错误。

这是代码。

服务( Angular )

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

组件( Angular )

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”。

Controller (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);
        }
    }

Angular this.postService.isOpen(这个api,工作完美)

不工作this.postService.setPost,我尝试删除所有内容以查看 map 是否像那样

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

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

最佳答案

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

关于java - Angular 的请求不要在后端映射我的 API(Spring Boot),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58766231/

相关文章:

angular - 我想将 angular2 observable 转换为一个类

java - SpringBoot : Configurable @Qualifier to select bean

java - 编写 orm.xml 的最佳实践

java - 如何定义类和接口(interface)层次结构?

java - 使用 java、Apache POI 每 3 列后在 Excel 中创建列标题

angular - Angular 9 中模板必须是字符串错误

angular - Http 提供商错误 Ionic 3

hibernate - 无法捕获带有包装事务的 DataIntegrityViolationException

java - 当同时使用方面和事务时,它是代理之上的代理吗?

java - 错误: E/LB: fail to open file: No such file or directory - Android Studio