2017-08-19 7 views
3

私はJava/ Spring MVC RESTful appで動作し、POSTリクエストを実行している間に400 HTTP status errorを取得します。 @RestController方法はHTTPステータス400 - 必須の文字列パラメータ 'walletName'が存在しません

@RequestMapping(value = "/generateAddress", method = RequestMethod.POST) 
    public ResponseEntity<WalletInfoWrapper> generateAddress(@RequestParam("walletName") String walletName, 
                  @RequestParam("currencyName") String currencyName) { 

     logger.info("walletName {} and currencyName {}", walletName, currencyName); 

     // return if the wallet name or the currency is null 
     if (Objects.isNull(walletName) || Objects.isNull(currencyName)) { 
      return new ResponseEntity<WalletInfoWrapper>(HttpStatus.NOT_ACCEPTABLE); 
     } 

     WalletInfo walletInfo = walletService.generateAddress(walletName, currencyName); 

     if (Objects.isNull(walletInfo)) { 
      return new ResponseEntity<WalletInfoWrapper>(HttpStatus.NOT_ACCEPTABLE); 
     } 

     WalletInfoWrapper walletInfoWrapper = new WalletInfoWrapper(); 
     walletInfoWrapper.setName(walletInfo.getName()); 

     return new ResponseEntity<WalletInfoWrapper>(walletInfoWrapper, HttpStatus.CREATED); 
    } 

以下に提供PostmanPOST要求、

enter image description here

エラーメッセージが通知、Required String parameter 'walletName' is not present

enter image description here

Iも提供することができ、提供されのコードおよびdataase層を含む。ここの問題は何ですか? I POST

UPDATE

は、私はこのようなPostmanリクエストを更新し、まだ同じエラーを持つ、

enter image description here

UPDATE 1

は、私はまだ同じ問題を持っていますデータでは、これは、エラーメッセージである

private class WalletWithMoneyRequest { 

     String walletName; 

     String currencyName; 

     public WalletWithMoneyRequest(String walletName, String currencyName) { 
      this.walletName = walletName; 
      this.currencyName = currencyName; 
     } 

     public WalletWithMoneyRequest() { 
     } 

     public String getWalletName() { 
      return walletName; 
     } 

     public String getCurrencyName() { 
      return currencyName; 
     } 

     public void setCurrencyName(String currencyName) { 
      this.currencyName = currencyName; 
     } 

     public void setWalletName(String walletName) { 
      this.walletName = walletName; 
     } 
    } 

コードが以下に提供される

{"walletName":"puut","currencyName":"Bitcoin"} 

@RequestMapping(value = "/generateAddress", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE) 
    public ResponseEntity<WalletInfoWrapper> generateAddress(@RequestBody WalletWithMoneyRequest walletWithMoneyRequest) { 

     String walletName = walletWithMoneyRequest.getWalletName(); 

     String currencyName = walletWithMoneyRequest.getCurrencyName(); 

     logger.info("walletName {} and currencyName {}", walletName, currencyName); 

     // return if the wallet name or the currency is null 
     if (Objects.isNull(walletName) || Objects.isNull(currencyName)) { 
      return new ResponseEntity<WalletInfoWrapper>(HttpStatus.NOT_ACCEPTABLE); 
     } 

     WalletInfo walletInfo = walletService.generateAddress(walletName, currencyName); 

     if (Objects.isNull(walletInfo)) { 
      return new ResponseEntity<WalletInfoWrapper>(HttpStatus.NOT_ACCEPTABLE); 
     } 

     WalletInfoWrapper walletInfoWrapper = new WalletInfoWrapper(); 
     walletInfoWrapper.setName(walletInfo.getName()); 

     return new ResponseEntity<WalletInfoWrapper>(walletInfoWrapper, HttpStatus.CREATED); 
    } 

設けられPOJO

enter image description here

?/ someurl walletName =:ここでは

org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver: 08/19/2017 19:45:55 - Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot construct instance of `mobi.puut.controllers.WalletRestController$WalletWithMoneyRequest` (although at least one Creator exists): can only instantiate non-static inner class by using default, no-argument constructor; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of `mobi.puut.controllers.WalletRestController$WalletWithMoneyRequest` (although at least one Creator exists): can only instantiate non-static inner class by using default, no-argument constructor 
at [Source: (PushbackInputStream); line: 1, column: 2] 

Tomcat Localhost log

enter image description here

Tomcat Catalina log enter image description here

+3

JSON文字列をパラメータとして送信しているようですが、これはコントローラが予期していないものです。それとも、郵便配達所でどのように見えるのですか? – tima

+0

コントローラがそれを理解する方法を教えてください。 – Arefe

+0

注:通常、RESTful要求はGETまたはPOSTパラメータを受け入れ、JSONを返します。 – Powerlord

答えて

2

あなたのコントローラは、通常、次のようになり2つのリクエストパラメータを期待して、Tomcatサーバー情報をイアmy-wallets-name & currencyName =ドル。

ポストボディにjsonストリングを送信していますが、仮パラメータはありません。 2つのエンドを一致させるには、POSTまたはコントローラを更新する必要があります。私はおそらく2つの@RequestParam注釈付き文字列を、2つのStringメンバーを持つJava pojoに置き換えたいと思うでしょう:walletNameとcurrencyName、pojoを引数としてリクエストメソッドに置き、アノテーション@RequestBodyを先行させます。これはあなたのjsonの投稿と一致します。お使いのコントローラがこのように身体の編集でそれをJSONでポストを受け入れているように

@RequestMapping(value = "/generateAddress", method = RequestMethod.POST) 
public ResponseEntity<WalletInfoWrapper> generateAddress(@RequestBody 
    WalletWithMoneyRequest myJsonRequestComingIn) { 
    logger.info("walletName {} and currencyName {}", myJsonRequestComingIn.getWalletName(), myJsonRequestComingIn.getCurrencyName()); 

そして、あなたのPOJO

public class WalletWithMoneyRequest{ 

    private String walletName; 
    private String currencyName; 

    //getters and setters down here. 
+0

私は明示的な回答を避けていました。新しいことを学ぶことを試みているなら、あなたは物事を理解する機会があります。私に教えてください。必要ならば、コード例で更新します。 – zerpsed

+0

コードサンプルはその答えに役立ちます。もしそうなら、 '(POSTかコントローラのどちらかを更新する)'両方の方法を提供して、正しく動作させてください。 – Arefe

+0

ok、コントローラーの更新を含むコード例を追加してください。 – zerpsed

1

編集

あなたの郵便配達の依頼で、 JSONを送信する代わりに、値をx-www-form-urlencodedとして送信します。

enter image description here

+0

問題は解決しません。 – Arefe

+1

@Artin同じエラーや別のもの? – tima

+0

前と同じエラーです。私は – Arefe

1

のzerpsed答えをについては詳しく説明し。

変更署名へ:WalletPOJO私は問題は今のところ解決され信じてwalletNameとcurrencyName

1

2つのフィールドがあり

public ResponseEntity<WalletInfoWrapper> generateAddress(@ResponseBody WalletPOJO walletCurrency) 

。私はRESTfulメソッドの@RequestBodyパラメータで提案されているようにPOJOを使用しました。ここでキャッチするのは、同じファイル内でクラスの外にPOJOを作成し、後でエンティティとしてエンティティディレクトリに配置する必要があることです。

class WalletWithMoneyRequest { 

    String walletName; 

    String currencyName; 

    public WalletWithMoneyRequest(String walletName, String currencyName) { 
     this.walletName = walletName; 
     this.currencyName = currencyName; 
    } 

    public WalletWithMoneyRequest() { 
    } 

    public String getWalletName() { 
     return walletName; 
    } 

    public String getCurrencyName() { 
     return currencyName; 
    } 

    public void setCurrencyName(String currencyName) { 
     this.currencyName = currencyName; 
    } 

    public void setWalletName(String walletName) { 
     this.walletName = walletName; 
    } 
} 

主な問題はHQLに誤りだったと信じている、

enter image description here

私はそれは私がまだのデータを持つことはできませんcurrency = :currency

あるべきcurrency =: currencyを書きましたデータベースには、database層のメソッドを変更する必要があります。