我已經在 Spring 中構建了一個 REST API,到目前為止運行良好。我現在想在我的請求中添加一個包含資料的正文。我的 REST API 端點,它等待請求中的正文資料,如下所示。
@RestController
public class TestController {
@GetMapping("/test")
public String Test(@RequestBody(required=true) String fullName) {
return "Hello " fullName;
}
}
我嘗試通過命令列呼叫端點,如下所示。
curl -X GET -H "Content-type: application/json" -d "John Doe" "http://localhost:8080/test"
這會導致以下結果并證明 REST API 作業正常。
Hello John Doe
無論如何,我無法在 Delphi 中完成它。
procedure TForm1.Button1Click(Sender: TObject);
var
RESTClient : TRESTClient;
RESTRequest : TRESTRequest;
RESTResponse : TRESTResponse;
begin
RESTClient := TRESTClient.Create(nil);
RESTClient.BaseURL := 'http://localhost:8080/test';
RESTResponse := TRESTResponse.Create(nil);
RESTRequest := TRESTRequest.Create(nil);
RESTRequest.Client := RESTClient;
RESTRequest.Response := RESTResponse;
RESTRequest.Execute;
if RESTResponse.Status.Success then
begin
ShowMessage(RESTResponse.Content);
end;
end;
有誰知道我如何實作這一目標?我非常感謝任何形式的幫助,純粹的!
我嘗試以多種不同的方式呼叫端點,如下所示。
// see above...
RESTRequest.ClearBody;
RESTRequest.Params.AddHeader('Content-Type', 'application/json');
RESTRequest.Body.Add('{"fullname": "John Doe"}');
RESTRequest.Execute;
可悲的是,這會導致以下錯誤。
DefaultHandlerExceptionResolver : Resolved [org.springframework.web.HttpMediaTypeNotSupportedException: Invalid mime type "application%2Fjson": does not contain '/']
uj5u.com熱心網友回復:
我天真的方法是:
RESTRequest.ClearBody;
RESTRequest.AddBody('John Doe');
RESTRequest.Execute;
這將制作一個普通的字串主體。如果你真的需要一個 JSON 正文,你可以寫
RESTRequest.AddBody('{"fullname": "John Doe"}', TRESTContentType.ctAPPLICATION_JSON);
但由于我對 Spring 不熟悉,我不確定這是否適合這里。
更新:更改服務器以接受 JSON 物件后,第二種方法似乎更好。在這種情況下,您甚至可以使用類似的 Delphi 類。
type
TPerson = class
private
FFirstname: string;
FLastname: string;
public
property Firstname: string read FFirstname write FFirstname;
property Lastname: string read FLastname write FLastname;
end;
person := TPerson.Create;
try
person.Firstname := 'John';
person.Lastname := 'Doe';
RESTRequest.AddBody<TPerson>(person);
finally
person.Free;
end;
uj5u.com熱心網友回復:
我已將方法從 GET 更改為 POST,因為我只會對 REST API 執行 POST 請求。不幸的是,這解決了我最初的問題,即使我不知道確切原因......無論如何,這里有一個快速演練。
首先,您必須創建一個類,Spring 稍后會使用該類使用 JSON 中的給定資料從中創建一個實體。請注意,此步驟是可選的,但非常推薦。
public class Person {
private String firstname;
private String lastname;
public Person (String firstname, String lastname) {
this.firstname = firstname;
this.lastname = lastname;
}
public String getFirstname() {
return firstname;
}
public String getLastname() {
return lastname;
}
}
更新后的端點如下所示。
@PostMapping(
value = "/test",
consumes = {MediaType.APPLICATION_JSON_VALUE} // awaits JSON
)
public String Test(@RequestBody Person person) {
return person.getFirstname()); // will return John
}
參考@Uwe Raabe 的回答,我在 Delphi 中呼叫 REST API 端點,如下所示。請注意,我在正文資料周圍添加了典型的 JSON 語法,這是必要的,因為沒有它 Spring 無法創建資料類。
// ...
RESTRequest.ClearBody;
RESTRequest.AddBody('{"firstname": "John", "lastname": "Doe"}', ctAPPLICATION_JSON);
RESTRequest.Execute;
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/377371.html
上一篇:Heroku上ReactJS應用程式中的內容安全策略
下一篇:防止TParams重復
