PUT 请求抛出 415 不支持的媒体类型:[无正文]

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

我在这方面没有太多经验,所以请原谅我。

我有一个简单的 PUT 请求,它接受带有整数 id 和 id 列表(从前端作为数组传递)的请求正文。

{ id: 1, itemIds: [10, 11, 12] }

当其余 API 接受此请求时,它看起来是正确的。我将其设置为 void 方法,并且不发送响应正文,仅发送 200。

故障发生在restTemplate.exchange这个方法中。我假设它何时到达 httpEntity(已修改以在此处发布):

  public void updateItems(final UpdateRequest updateRequest) {
    UriComponents uriBuilder =
        UriComponentsBuilder
            .fromHttpUrl(baseUrl + UPDATE_PATH)
            .build();
    HttpHeaders headers = new HttpHeaders();
    headers.set(HttpHeaders.ALLOW, HttpMethod.PUT.name());
    headers.set(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE);
    headers.set(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
    HttpEntity<UpdateRequest> httpEntity =
        new HttpEntity<>(updateRequest, headers);
  
    try {
      restTemplate
          .exchange(
              uriBuilder.toUriString(),
              HttpMethod.PUT,
              httpEntity,
              new ParameterizedTypeReference<Void>() {});
    } catch (HttpStatusCodeException e) {
      throw handleHttpClientErrorException(e);
    }
  }

我快速查看了服务器日志,看看是否可以找出发生此错误的位置(已修改以在此处发布):

2024-05-15 21:20:26.030 INFO  71833 [da4509e7-9d23-4a49-9565-7345937c2ab1] [Pf7lpNNjRcShs6YA0Dkw5A] [app] [/api/my-endpoint] - [utor-thread-100] com.ha.rest.util.RestLoggingHelper - restType=REQUEST|requestMethod=PUT|requestUri=/app/api/my-endpoint|requestHeaders=[[{Accept-Language:en-US,en;q=0.9,Accept-Encoding:gzip, deflate, br, zstd,Referer:http://localhost:9083/app/ui/,Sec-Fetch-Dest:empty,Sec-Fetch-Mode:cors,Sec-Fetch-Site:same-origin,sec-ch-ua-platform:"Windows",User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36,sec-ch-ua-mobile:?0,Content-Type:application/json,Accept:application/json, text/plain, */*,sec-ch-ua:"Not_A Brand";v="8", "Chromium";v="120", "Google Chrome";v="120",Content-Length:45,Connection:close,Host:localhost:9080,}]]|requestBody=["Check response log, contentLength=45"]
2024-05-15 21:20:30.184 DEBUG 75987 [da4509e7-9d23-4a49-9565-7345937c2ab1] [Pf7lpNNjRcShs6YA0Dkw5A] [app] [/api/my-endpoint] - [utor-thread-100] com.ha.rest.RestTemplate - HTTP PUT http://service-v1/service/v1/my-endpoint
2024-05-15 21:20:30.184 DEBUG 75987 [da4509e7-9d23-4a49-9565-7345937c2ab1] [Pf7lpNNjRcShs6YA0Dkw5A] [app] [/api/my-endpoint] - [utor-thread-100] com.ha.rest.RestTemplate - Accept=[application/json, application/*+json]
2024-05-15 21:20:30.184 DEBUG 75987 [da4509e7-9d23-4a49-9565-7345937c2ab1] [Pf7lpNNjRcShs6YA0Dkw5A] [app] [/api/my-endpoint] - [utor-thread-100] com.ha.rest.RestTemplate - Writing [UpdateRequest{id=13938, itemIds=[108067]}] as "application/json"
2024-05-15 21:20:30.216 INFO  76019 [da4509e7-9d23-4a49-9565-7345937c2ab1] [QdMxDbpHTKWZnulKulVAYA] [app] [/api/my-endpoint] - [utor-thread-100] com.ha.rest.util.RestLoggingHelper - restType=REQUEST|requestMethod=PUT|requestUri=http://service-v1/service/v1/my-endpoint|discoveredRequestUri=http://-.com:11492/service/v1/my-endpoint|requestBody=[{"id":13938,"itemIds":[108067]}]|requestHeaders={x-url-encoded-single:[true],}|requestMaxReadTimeOut=300000
2024-05-15 21:20:30.216 INFO  76019 [da4509e7-9d23-4a49-9565-7345937c2ab1] [QdMxDbpHTKWZnulKulVAYA] [app] [/api/my-endpoint] - [utor-thread-100] com.ha.rest.util.RestLoggingHelper - restType=RESPONSE|requestUri=http://service-v1/service/v1/my-endpoint|responseCode=415 UNSUPPORTED_MEDIA_TYPE|responseStatus=Unsupported Media Type|responseBody=[null]
2024-05-15 21:20:30.216 DEBUG 76019 [da4509e7-9d23-4a49-9565-7345937c2ab1] [] [app] [/api/my-endpoint] - [utor-thread-100] com.RestTemplate - Response 415 UNSUPPORTED_MEDIA_TYPE
2024-05-15 21:20:30.266 ERROR 76069 [da4509e7-9d23-4a49-9565-7345937c2ab1] [] [app] [/api/my-endpoint] - [utor-thread-100] app.utils.ExceptionUtils - JSON Processing Exception

我找了一整天,似乎找不到问题出在哪里。任何指导将不胜感激。

api rest request put
1个回答
0
投票

415 响应可能是因为您没有将正文编码为 JSON。

您发送的是普通对象而不是 JSON 对象。

您需要使用一个包将其解析为 JSON。尝试 Jackson 或 GSON

https://stackoverflow.com/a/31743324/8564731

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