请求后不支持的媒体类型

时间:2019-03-24 08:27:39

标签: java angular spring typescript spring-rest

我想从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;
    .....
}

2 个答案:

答案 0 :(得分:0)

按以下方式提供PostMapping中的内容类型:

public class Post extends AsyncTask<Void,Void,String> {

    private static String latitude, longitude;
    final Handler handler = new Handler();
    static Timer timer;
    Context context;
    ProgressDialog progressDialog;
    private TimerTask doAsynchronousTask;

    public Post(MainActivity context) {
        this.timer = timer;
        this.context = context;
        progressDialog = new ProgressDialog(context);
    }

    @Override
    protected void onPreExecute() {
        progressDialog.setTitle("Connecting");
        progressDialog.setMessage("Trying to connect to your server. Please wait...");
        progressDialog.setCancelable(false);
        progressDialog.setCanceledOnTouchOutside(false);
        progressDialog.show();
    }

    @Override
    protected String doInBackground(Void... voids) {
        doTimerTask();
    }

    @Override
    protected void onPostExecute(String response) {
        if (progressDialog.isShowing()) {
            progressDialog.dismiss();
        }
    }

    private void doTimerTask(String trackingTimeInterval) {
        doAsynchronousTask = new TimerTask() {
            @Override
            public void run() {
                handler.post(new Runnable() {
                    @SuppressWarnings("unchecked")
                    public void run() {
                        String msg;
                        if (Post.latitude != null && Post.longitude != null) {
                            System.out.println(Post.latitude + "|" + Post.longitude);
                        }
                    }
                });
            }
        };
        timer.schedule(doAsynchronousTask, 0, 120000);
    }

}

这里“ application / json”只是一个示例。您可以输入合适的类型。

答案 1 :(得分:0)

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

415 Unsupported MediaType for POST request in spring application