2017-08-02 98 views
0

我使用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); 
     } 

    } 

CurlPOST请求,

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 walletNameString 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

+1

发布您的get方法处理程序,以及如何通过curl发送获取请求 – TruckDriver

+0

我已经为get处理程序提供了'cURL'请求。 Cna提供了更多的信息,如果需要的话 – Arefe

回答

1

当前您正在返回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); 
} 

如果你只是想要的地址,那么你的包装只能有地址字段。 另外,如果你是不屑关于每个双引号前加“\”,那是因为你重定向从REST调用输出到一个JSON实用

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

,您可以通过只

看到平原输出
curl -G http://localhost:8080/rest/wallets/1 
+0

如果我提出类似'curl -H“的请求,就可以工作Content-Type:application/json”-X POST -d“nonald”http:// localhost:8080/rest/generateAddress'.It清楚的'字符串' – Arefe