I work with a Spring RESTful app and tries to do a POST request. It posts the data, but, when I tried to GET it back, the data format seems not in the correct format. The method is provided below,
@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); } } The POST request in Curl,
curl -H "Content-Type: application/json" -X POST -d '{"name":"mikiii"}' http://localhost:8080/rest/generateAddress I get the data using the GET,
[ // more data { "id": 16, "name": "{\"name\":\"mikiii\"}", "address": "mvmHyU1k6qkoUEpM9CQg4kKTzQ5si3oR1e" } ] If I use only a String,
curl -H "Content-Type: application/json" -X POST -d '"muuul"' http://localhost:8080/rest/generateAddress I get it back as
[ // ........., { "id": 17, "name": "\"muuul\"", "address": "mr9ww7vCUzvXFJ6LDAz6YHnHPsd9kgYfox" } ] This is the inner implementation of the generateAddress method (I have the same name, should have changed) create a new WalletInfo entity and currently returns 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; } Currently, the generateAddress returns void. Earlier, I tried to return the WalletInfo from the method and set the location using the code, httpHeaders.setLocation(uriComponentsBuilder.path("/wallets/{id}").buildAndExpand(walletInfo.getId()).toUri());
I get an error and this seems not working. I can provide that error stack if necessary, but, now again I return the void from the method in the current code.
In the RESTful method level, I have tried with @PathVariable("name") String walletName and String walletName. Obvously, this didn't help out and provided errors.
UPDATE
The GET method handlers with the Curl request are provided below,
// 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); } How to get the address in the clean String like "name": "testuser" and have a proper POST request?
cURLrequests. Cna provided more info if required