为什么Angular HttpClient响应为空?

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

我的环境:

  • Shop RestController:Spring Boot 在localhost laptopn ubuntu 18:04
  • ShopService:Angular HttpClient 在localhost桌面PC窗口10

问题:

我通过HttpClient获取请求得到了响应null。但是,如果我使用邮递员或浏览器,我得到了我期望的响应机构。

角度代码:

const BASE_URL = 'http://pc01:8081/shopsystem/shops';
const  SHOP_NAME = 'Muster Shop';

@Injectable({
  providedIn: 'root'
})
export class ShopService {

  shop$: Shop;
  address$: Address;
  owner$: Owner;

  //header = new HttpHeaders({ 'Content-Type': 'application/json' });

  constructor(private http: HttpClient) { }

  public getShop(): Observable<Shop>{
    return this.http.get<Shop>(BASE_URL + '/' + SHOP_NAME);
  }

响应头:

enter image description here

响应:

enter image description here

邮递员要求:

enter image description here

Spring Boot控制器:

enter image description here

我已经尝试使用带有application / json但具有相同响应的任何和HttpHeaders的Observable。

另一件事:如果我的Spring Boot控制器返回一个List,那么我得到了包含内容的响应体。

题:

为什么我的HttpClient请求中有空或空体?

我怎么解决这个问题?

谢谢

java angular spring-boot httpclient angular7
1个回答
1
投票

你的Angular HttpClient对路径变量中的空间进行编码,因此它作为Muster%20Shop来到RestController。

然后,您尝试查找具有该名称的记录,该记录不存在。

在将@PathVariable作为Muster Shop发送给您的查询之前,您必须对其进行解码。

© www.soinside.com 2019 - 2024. All rights reserved.