Are*_*efe 5 java rest hibernate spring-mvc
我使用Java/ Spring MVC RESTfulapp并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);
}
Run Code Online (Sandbox Code Playgroud)
所述POST请求中的Postman下方,
错误消息通知, Required String parameter 'walletName' is not present
我还可以为观察下拉操作services的dataase层和层提供代码.这是什么问题?
UPDATE
我更新了Postman这样的请求,仍然有同样的错误,
UPDATE 1
我还有同样的问题,
我POST有数据,
{"walletName":"puut","currencyName":"Bitcoin"}
Run Code Online (Sandbox Code Playgroud)
代码如下,
@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);
}
Run Code Online (Sandbox Code Playgroud)
该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;
}
}
Run Code Online (Sandbox Code Playgroud)
这是错误消息,
这里是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]
Run Code Online (Sandbox Code Playgroud)
Tomcat Localhost log
您的控制器需要 2 个请求参数,通常如下所示:/someurl?walletName=my-wallets-name¤cyName=dollars。
您在帖子正文中发送 json 字符串,但没有形式参数。您需要更新您的 POST 或控制器以使两端一致。我认为您可能想用一个具有两个字符串成员的 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());
Run Code Online (Sandbox Code Playgroud)
还有你的波乔
public class WalletWithMoneyRequest{
private String walletName;
private String currencyName;
//getters and setters down here.
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14046 次 |
| 最近记录: |