使用ResponseEntity获取Http响应

布莱恩·奥(Brian O):

我创建了一个包含以下代码行的类。

private String app_key = "XXXXXXXXXXXXXXXXX";
private String app_secret = "XXXXXXXXXXXXXX";
private String appKeySecret = app_key + ":" + app_secret;
private byte[] bytes = appKeySecret.getBytes(StandardCharsets.ISO_8859_1);
private String auth = Base64.encode(bytes);

private OkHttpClient client = new OkHttpClient();

private Request request = new Request.Builder()
        .url("https://sandbox.safaricom.co.ke/oauth/v1/generate?grant_type=client_credentials")
        .get()
        .addHeader("authorization", "Basic " + auth)
        .addHeader("cache-control", "no-cache")
        .build();

Response response = client.newCall(request).execute();

我也有一个带有“获取映射”的控制器,看起来像这样

  @GetMapping("/auth_token")
public ResponseEntity<MpesaAuth> getToken(@RequestBody Mauth mAuth){
    return new ResponseEntity<>(mpesaAuth, HttpStatus.OK);
}

但是当我在邮递员上测试端点时,出现以下错误

    "timestamp": "2019-09-25T17:26:07.522+0000",
"status": 400,
"error": "Bad Request",
"message": "Required request body is missing: public org.springframework.http.ResponseEntity<com.findfix.payment.config.Mauth> com.findfix.payment.controllers.PaymentController.getToken(com.findfix.payment.config.MpesaAuth)",
"path": "/api/mypayment/v1/auth_token"
}

我可能会错过什么。提前致谢。

死侍 :

Get方法将没有身体,所以@RequestBody Mauth mAuthgetToken

@GetMapping("/auth_token")
public ResponseEntity<MpesaAuth> getToken(){
    return new ResponseEntity<>(mpesaAuth, HttpStatus.OK);
}

如果要通过身体,则需要使用@PostMapping@PutMapping

@PostMapping("/auth_token") // or @PutMapping
public ResponseEntity<MpesaAuth> getToken(@RequestBody Mauth mAuth){
    return new ResponseEntity<>(mpesaAuth, HttpStatus.OK);
 }

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章