我使用Spring RESTful
應用程序並試圖執行POST
請求。它發佈數據,但是,當我試圖回到GET
時,數據格式似乎沒有正確的格式。以下提供的方法,Spring RESTful應用程序中的HTTP POST請求
@RestController
@RequestMapping("/rest")
@Produces({"text/plain", "application/xml", "application/json"})
public class WalletRestController {
@Autowired
private WalletService walletService;
@PostMapping("/generateAddress")
public ResponseEntity<Void> generateAddress(@RequestBody String walletName,
UriComponentsBuilder uriComponentsBuilder) {
System.out.println("Creating wallet with name = " + walletName);
if (walletName == null) {
return new ResponseEntity<Void>(HttpStatus.NOT_ACCEPTABLE);
}
walletService.generateAddress(walletName);
HttpHeaders httpHeaders = new HttpHeaders();
// httpHeaders.setLocation(uriComponentsBuilder.path("/wallets/{id}").buildAndExpand(walletInfo.getId()).toUri());
return new ResponseEntity<Void>(httpHeaders, HttpStatus.CREATED);
}
}
在Curl
的POST
請求,
curl -H "Content-Type: application/json" -X POST -d '{"name":"mikiii"}' http://localhost:8080/rest/generateAddress
我使用GET
數據得到,
[
// more data
{
"id": 16,
"name": "{\"name\":\"mikiii\"}",
"address": "mvmHyU1k6qkoUEpM9CQg4kKTzQ5si3oR1e"
}
]
如果我只使用一個String
,
curl -H "Content-Type: application/json" -X POST -d '"muuul"' http://localhost:8080/rest/generateAddress
我拿回來的
[
// .........,
{
"id": 17,
"name": "\"muuul\"",
"address": "mr9ww7vCUzvXFJ6LDAz6YHnHPsd9kgYfox"
}
]
這是內實現generateAddress
方法(I have the same name, should have changed)
創建一個新的WalletInfo
實體,目前返回void
,
@Override
public synchronized void generateAddress(final String walletName) {
WalletInfo walletInfo = walletInfoDao.getByName(walletName);
// generate wallet, if the wallet is not
// generated previously
if (walletInfo == null) {
if (genWalletMap.get(walletName) == null) {
final WalletManager walletManager = WalletManager.setupWallet(walletName);
walletManager.addWalletSetupCompletedListener((wallet) -> {
Address address = wallet.currentReceiveAddress();
WalletInfo newWallet = createWalletInfo(walletName, address.toString());
walletMangersMap.put(newWallet.getId(), walletManager);
genWalletMap.remove(walletName);
});
genWalletMap.put(walletName, walletManager);
// return walletInfo;
}
}
// return null;
}
目前,generateAddress
回報void
。早些時候,我試圖從該方法返回WalletInfo
和使用的代碼中設置的位置, httpHeaders.setLocation(uriComponentsBuilder.path("/wallets/{id}").buildAndExpand(walletInfo.getId()).toUri());
我得到一個錯誤,這似乎不工作。如有必要,我可以提供該錯誤堆棧,但是,現在我再次從當前代碼中的方法返回void
。
在RESTful
方法級別,我嘗試了@PathVariable("name") String walletName
和String walletName
。很顯然,這並沒有幫助 並提供了錯誤。
UPDATE
與下面提供的Curl
請求GET
方法處理,
// curl -G http://localhost:8080/rest/wallets | json
@RequestMapping(value = "/wallets", method = RequestMethod.GET)
public ResponseEntity<List<WalletInfo>> getAllWalletInfo() {
List<WalletInfo> walletInfos = walletService.getAllWallets();
if (Objects.isNull(walletInfos)) {
return new ResponseEntity<List<WalletInfo>>(HttpStatus.NO_CONTENT);
}
return new ResponseEntity<List<WalletInfo>>(walletInfos,
HttpStatus.OK);
}
// curl -G http://localhost:8080/rest/wallets/1 | json
@RequestMapping(value = "/wallets/{id}", method = RequestMethod.GET)
public ResponseEntity<WalletInfo> getWalletById(@PathVariable("id") long id) {
System.out.println("Get wallet info with Id = " + id);
WalletInfo walletInfo = walletService.getWalletInfo(id);
if (walletInfo == null) {
return new ResponseEntity<WalletInfo>(HttpStatus.NOT_FOUND);
}
return new ResponseEntity<WalletInfo>(walletInfo, HttpStatus.OK);
}
如何獲得在清潔String
地址一樣"name": "testuser"
和有請求正確POST
?
發佈您的get方法處理程序,以及如何通過curl發送獲取請求 – TruckDriver
我已經爲get處理程序提供了'cURL'請求。 Cna提供了更多的信息,如果需要的話 – Arefe