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
HTTP Status 400 - Required String parameter 'walletName' is not present
提问by Chaklader Asfak Arefe
I work with Java/ Spring MVC RESTful
app and get 400 HTTP status error
while doing a POST
request. The @RestController
method 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 POST
request in the Postman
provided below,
下面提供的POST
请求Postman
,
The error message informs, Required String parameter 'walletName' is not present
错误消息通知, Required String parameter 'walletName' is not present
I can also provide the code for the services
and the dataase
layers for observing the drop-down operations. What is the issue here?
我还可以提供用于观察下拉操作services
的dataase
层和层的代码。这里有什么问题?
UPDATE
UPDATE
I updated the Postman
request like this and still having the same error,
我Postman
像这样更新了请求,但仍然出现相同的错误,
UPDATE 1
UPDATE 1
I still have the same issue,
我还是有同样的问题
I POST
with 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 POJO
is 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,
这是错误信息,
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
采纳答案by zerpsed
Your controller is expecting 2 request parameters that normally look like this: /someurl?walletName=my-wallets-name¤cyName=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
回答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 POJO
as suggested with the @RequestBody
parameter in the RESTful
method. The catch here is I need to make the POJO
out of the class (in the same file though) and later, put in the entity directory as an entity.
我相信这个问题现在已经解决了。我已经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;
}
}
The main issue is believe was an error in the HQL
,
主要问题是相信是错误的HQL
,
I wrote currency =: currency
where 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 database
layer.
我仍然无法在数据库中拥有数据,因为我需要修改database
图层中的方法。
回答by Paul Guacan
En POSTMAN set variables in params
En POSTMAN 在 params 中设置变量