2017-08-02 55 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