gpt4 book ai didi

java - Spring RESTful 应用程序中的 HTTP POST 请求

转载 作者:行者123 更新时间:2023-12-02 12:26:23 25 4
gpt4 key购买 nike

我使用 Spring RESTful 应用程序并尝试执行 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"
}
]

如果我只使用字符串

curl -H "Content-Type: application/json" -X POST -d '"muuul"' http://localhost:8080/rest/generateAddress

我得到它作为

[
// .........,
{
"id": 17,
"name": "\"muuul\"",
"address": "mr9ww7vCUzvXFJ6LDAz6YHnHPsd9kgYfox"
}
]

这是 generateAddress 方法的内部实现(我有相同的名称,应该已更改) 创建一个新的 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 walletNameString walletName。显然,这没有帮助并提供了错误。

更新

下面提供了带有 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请求?

最佳答案

当前您正在返回 WalletInfo 作为响应实体 getWalletById ()。

return new ResponseEntity<WalletInfo>(walletInfo, HttpStatus.OK);

所以这个WalletInfo将被jackson mapper转换,并且WalletInfo中的相应字段将作为json对象返回。我猜你的 WalletInfo 类中有 id、名称和地址字段。如果您只想返回这些字段的子集,例如名称和地址,则创建一个包装类,例如

public class WalletInfoWrapper {
String name;
String address;
.. //gettter , setter
}

并从您的处理程序返回此类的对象,因此您的 get 方法处理程序的新代码将如下所示

@RequestMapping(value = "/wallets/{id}", method = RequestMethod.GET)
public ResponseEntity<WalletInfoWrapper > 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);
}
WalletInfoWrapper walletInfoWrapper = new WalletInfoWrapper ();
walletInfoWrapper.setName(walletInfo.getName());
walletInfoWrapper.setAddress(walletInfo.getAddress());
return new ResponseEntity<WalletInfoWrapper >(walletInfoWrapper , HttpStatus.OK);
}

如果您只想要地址,那么您的包装器只能有地址字段。另外,如果您对每个双引号之前的“\”感到困扰,那是因为您将其余调用的输出重定向到 json 实用程序

curl -G http://localhost:8080/rest/wallets/1 | json

,您只需

即可看到纯输出
curl -G http://localhost:8080/rest/wallets/1

关于java - Spring RESTful 应用程序中的 HTTP POST 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45453789/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com