繁体   English   中英

HTTP 状态 400 - 所需的字符串参数“walletName”不存在

[英]HTTP Status 400 - Required String parameter 'walletName' is not present

我使用Java/ Spring MVC RESTful应用程序并在执行POST请求时收到400 HTTP status error 提供了@RestController方法,

@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请求,

在此处输入图片说明

错误消息通知, Required String parameter 'walletName' is not present

在此处输入图片说明

我还可以提供用于观察下拉操作的servicesdataase层的代码。 这里有什么问题?

UPDATE

我像这样更新了Postman请求,但仍然出现相同的错误,

在此处输入图片说明

UPDATE 1

我还是有同样的问题

我用数据POST

{"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

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;
        }
    }

这是错误信息,

在此处输入图片说明

这里是Tomcat服务器信息,

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

在此处输入图片说明

Tomcat Catalina log 在此处输入图片说明

编辑

在您的 Postman 请求中,不要发送 JSON,而是将值作为x-www-form-urlencoded

在此处输入图片说明

您的控制器需要 2 个通常如下所示的请求参数:/someurl?walletName=my-wallets-name&currencyName=dollars。

您在帖子正文中发送一个 json 字符串,但没有形式参数。 您需要更新您的 POST 或您的控制器以使两端一致。 我认为您可能想用一个 Java pojo 替换两个带 @RequestParam 注释的字符串,该 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. 

详细说明 zerpsed 的回答。

将签名更改为:

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

WalletPOJO 有两个字段 walletName 和 currencyName

我相信这个问题现在已经解决了。 我按照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的错误,

在此处输入图片说明

我写了currency =: currency ,它应该是currency = :currency

我仍然无法在数据库中拥有数据,因为我需要修改database层中的方法。

En POSTMAN 在 params 中设置变量在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM