请求后不支持的媒体类型

问题描述 投票:-1回答:2

我想从Angular客户端发出POST请求。我有这个Spring端点:

    @RestController()
public class HomeController {

  @PostMapping(value = "/payment/{unique_transaction_id}")
  public ResponseEntity<WpfResponse> handleWpfMessage(@PathVariable("unique_transaction_id") String unique_transaction_id,
      @RequestBody WpfPaymentsDTO transaction, HttpServletRequest request) throws Exception {

    /// .... 
    MessageProcessor messageProcessor = processors.getOrDefault(transaction.getTransaction_type(), defaultProcessor);
    return ResponseEntity.ok(messageProcessor.processMessage(merchant, contract, terminal.get(), transaction, request));
  }
}

我用这个打字稿代码:

save(main: MainForm, hash: string): void {
    const headers = new HttpHeaders();
    headers.append('Accept', 'application/json');
    headers.append('Content-Type', 'application/json');
    this.http.post('http://localhost:8080/some_package/payment/ckjzqtyxh5pnavbjecujhuvzaa5q8n74', JSON.stringify(main), { headers }).subscribe();
  }

但是当我运行代码时,我得到:

HttpErrorResponse {headers: HttpHeaders, status: 415, statusText: "Unsupported Media Type", url: "http://localhost:8080/some_package/payment/ckjzqtyxh5pnavbjecujhuvzaa5q8n74", ok: false, …}error: nullheaders: HttpHeaders {normalizedNames: Map(0), lazyUpdate: null, lazyInit: ƒ}lazyInit: ƒ ()lazyUpdate: nullnormalizedNames: Map(0) {}__proto__: Objectmessage: "Http failure response for http://localhost:8080/some_package/payment/ckjzqtyxh5pnavbjecujhuvzaa5q8n74: 415 Unsupported Media Type"name: "HttpErrorResponse"ok: falsestatus: 415statusText: "Unsupported Media Type"url: "http://localhost:8080/some_package/payment/ckjzqtyxh5pnavbjecujhuvzaa5q8n74"__proto__: HttpResponseBase

你知道我怎么解决这个问题吗?

WpfPaymentsDTO:

public class WpfPaymentsDTO {

    private String transaction_id;

    private String transaction_type;

    private String currency;

    private Integer amount;
    .....
}
java angular spring typescript spring-rest
2个回答
0
投票

在PostMapping中给出内容类型,如下所示:

@PostMapping(path = "/payment/{unique_transaction_id}", consumes = "application/json", produces = "application/json")

这里的“application / json”只是一个例子。你可以给出合适的类型。


0
投票

检查是否在Spring应用程序中添加并配置了Jackson。请参阅此问题:

415 Unsupported MediaType for POST request in spring application

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