【问题标题】:Unsupported Media Type after request请求后不支持的媒体类型
【发布时间】:2019-03-24 08:27:38
【问题描述】:

我想从 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


    【解决方案1】:

    在 PostMapping 中给出内容类型:

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

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

    【讨论】:

    • 解决不了问题。其他建议?
    • 我发现了问题。 nI 删除 @RequestBody WpfPaymentsDTO transaction 它工作正常。你知道如何解决这个问题吗?
    【解决方案2】:

    检查您是否在 Spring 应用程序中添加并配置了 Jackson。参考这个问题:

    415 Unsupported MediaType for POST request in spring application

    【讨论】:

    • 不,我找不到解决方案。
    • 拒绝什么?你的类路径中有杰克逊吗?你在 Spring 配置中配置了吗?
    猜你喜欢
    • 2015-06-17
    • 2018-10-13
    • 2016-11-21
    • 1970-01-01
    • 1970-01-01
    • 2021-12-01
    • 2013-01-30
    • 1970-01-01
    • 2017-07-16
    相关资源
    最近更新 更多