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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45771870/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 08:53:42  来源:igfitidea点击:

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

javahibernaterestspring-mvc

提问by Chaklader Asfak Arefe

I work with Java/ Spring MVC RESTfulapp and get 400 HTTP status errorwhile doing a POSTrequest. The @RestControllermethod is provided,

我使用Java/ Spring MVC RESTful应用程序并400 HTTP status error在执行POST请求时获取。@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);
    }

The POSTrequest in the Postmanprovided below,

下面提供的POST请求Postman

enter image description here

在此处输入图片说明

The error message informs, Required String parameter 'walletName' is not present

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

enter image description here

在此处输入图片说明

I can also provide the code for the servicesand the dataaselayers for observing the drop-down operations. What is the issue here?

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

UPDATE

UPDATE

I updated the Postmanrequest like this and still having the same error,

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

enter image description here

在此处输入图片说明

UPDATE 1

UPDATE 1

I still have the same issue,

我还是有同样的问题

I POSTwith the data,

POST用数据,

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

The code is provided below,

下面提供了代码,

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

The POJOis provided,

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

This is the error message,

这是错误信息,

enter image description here

在此处输入图片说明

Here ia the Tomcat server info,

这里是 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.Hymanson.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 Localhost log

enter image description here

在此处输入图片说明

Tomcat Catalina logenter image description here

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

采纳答案by zerpsed

Your controller is expecting 2 request parameters that normally look like this: /someurl?walletName=my-wallets-name&currencyName=dollars.

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

You're sending a json string in the post body, but no formal parameters. You need to update either your POST, or your controller to make the two ends agree. I think you probably want to replace the two @RequestParam annotated Strings, with a Java pojo that has two String members: walletName and currencyName, drop that pojo in your request method as an argument and precede it with the annotation @RequestBody. This will match your json post.

您在帖子正文中发送一个 json 字符串,但没有形式参数。您需要更新您的 POST 或您的控制器以使两端一致。我认为您可能想用一个 Java pojo 替换两个带 @RequestParam 注释的字符串,该 Java pojo 具有两个字符串成员:walletName 和 currencyName,将该 pojo 作为参数放入您的请求方法中,并在其前面加上注释 @RequestBody。这将匹配您的 json 帖子。

To have your controller accept the post with JSON in the body edit it like this:

要让您的控制器接受正文中带有 JSON 的帖子,请像这样编辑它:

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

And your pojo

还有你的 pojo

public class WalletWithMoneyRequest{ 

    private String walletName;
    private String currencyName;

    //getters and setters down here. 

回答by tima

Edit

编辑

In your Postman request, instead of sending JSON, send the values as x-www-form-urlencoded.

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

enter image description here

在此处输入图片说明

回答by Kim Rasmussen

To elaborate on zerpsed's answer.

详细说明 zerpsed 的回答。

Change the signature to:

将签名更改为:

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

Where the WalletPOJO has the two fields walletName and currencyName

WalletPOJO 有两个字段 walletName 和 currencyName

回答by Chaklader Asfak Arefe

I believe the issue is solved for now. I have used a POJOas suggested with the @RequestBodyparameter in the RESTfulmethod. The catch here is I need to make the POJOout of the class (in the same file though) and later, put in the entity directory as an entity.

我相信这个问题现在已经解决了。我已经POJO按照建议使用@RequestBodyRESTful方法中的参数。这里的问题是我需要制作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;
    }
}

The main issue is believe was an error in the HQL,

主要问题是相信是错误的HQL

enter image description here

在此处输入图片说明

I wrote currency =: currencywhere it should be currency = :currency

我写currency =: currency了它应该在的地方currency = :currency

I still can't have the data in the database as I will need to modify the method in the databaselayer.

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

回答by Paul Guacan

En POSTMAN set variables in params enter image description here

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