HTTP状态400 - 必需字符串参数'walletName'不存在

时间:2017-08-19 13:03:52

标签: java hibernate rest spring-mvc

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

下面提供的POST中的Postman个请求

enter image description here

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

enter image description here

我还可以提供servicesdataase图层的代码,以便观察下拉操作。这是什么问题?

UPDATE

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

enter image description here

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

这是错误消息,

enter image description here

这里是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

enter image description here

Tomcat Catalina log enter image description here

5 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:2)

修改

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

enter image description here

答案 2 :(得分:1)

详细说明zerpsed的答案。

将签名更改为:

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

WalletPOJO有两个字段walletName和currencyName

答案 3 :(得分:1)

我认为这个问题现在已经解决了。我已根据POJO方法中的@RequestBody参数使用了RESTful。这里的问题是我需要将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图层中的方法。

答案 4 :(得分:0)

在POSTMAN中以参数设置变量 enter image description here