【发布时间】:2018-01-09 06:18:52
【问题描述】:
我使用Spring RESTful 应用程序并尝试执行POST 请求。它发布了数据,但是,当我尝试返回 GET 时,数据格式似乎不正确。方法如下,
@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 请求?
【问题讨论】:
标签: java json rest spring-mvc spring-boot