tomcat 休息后端 Angular 前端邮递员

问题描述 投票:0回答:1

我在使用带有角度前端的 tomcat POST 服务时遇到问题。我对 Angular 还很陌生。 我的环境

  • tomcat 9.0
  • jdk 17
  • 具有独立组件的 Angular 17
  • javax.ws.rs框架
  • 日食 2023-06
  • 与代码 1.85.1

我已经尝试过 Postman,服务响应符合预期。 服务器端。我在 System.out... 和 return Response 上都有断点... 当我使用邮递员时,我在两个断点处停止。当使用有角度的前端时。我什么也没得到,甚至 eclipse 控制台也是空白的。我本以为会出现某种异常,但什么也没得到。

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.TEXT_PLAIN)
    @Path("/tag/add")
    public Response addTag(AddTag tag) {
        System.out.println(tag);
        return Response.status(200).entity("new tag: " + tag).build();

    }

组件.html

<div class="tagForm">
    <form [formGroup]="filterForm">
        <mat-form-field>
            <mat-label>Namn på ny tag</mat-label>
            <input type="text" matInput formControlName="tagName">
        </mat-form-field>
        <button mat-button (click)="addTag()">Lägg till</button>
    </form>
</div>

组件.ts

  addTag() {
    let name = this.tagName?.value
    let tagName = <addTag>({
      name: name,
      id: this.imageId
    });

    console.log("addTag: " + tagName);
    this.photoalbumService.addTag(tagName);
  }

相册.service.ts

  addTag(data: addTag): Observable<string> {
    const httpOptions = {
      headers: ({ 'Content-Type': 'application/json' })
    }
    let jsonData = JSON.stringify(data);
    console.log(jsonData);
    const url = baseURL + "/tag/add";
    console.log("addTag, url: " + url);
    return this.httpClient.post<string>(url, jsonData, httpOptions);

  }

我从环境(environment.dev.ts)文件中获取baseURL。

export const environment = {
    production: "dev",
    serverBaseUrl: "http://localhost:8080/photoalbumservice/rest/api",
    imageBaseUrl: "http://localhost:8080"
};

我尝试过邮递员,并且有效。

http://localhost:8080/photoalbumservice/rest/api/tag/add

我将以下内容作为原始正文发送。

{ "id": 123, "name":"kalle"}

控制台登录 vs code 如下。

addTag: [object Object] tag.component.ts:69:12
{"name":"jhkl","id":413791} photoalbum.service.ts:73:12
addTag, url: http://localhost:8080/photoalbumservice/rest/api/tag/add photoalbum.service.ts:75:12

我很确定我错过了一些需要的信息,所以如果您需要更多信息,我会尽力提供。

欢迎任何帮助。

angular rest tomcat postman
1个回答
0
投票

调用addTag()时尝试订阅返回的Observable。

this.photoalbumService.addTag(tagName).subscribe({
  next: value => console.log('Observable emitted the next value: ' + value),
  error: err => console.error('Observable emitted an error: ' + err),
  complete: () => console.log('Observable emitted the complete notification')
});
© www.soinside.com 2019 - 2024. All rights reserved.