MethodArgumentTypeMismatchException在Spring中
我使用此方法从数据库检索数据:MethodArgumentTypeMismatchException在Spring中,spring,spring-boot,xmlhttprequest,Spring,Spring Boot,Xmlhttprequest,我使用此方法从数据库检索数据: @GetMapping(path="/login/in", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE) User loginA(@RequestBody LoginCredential newLogin) { logger.debug(newLogin); return repository.findByEmail
@GetMapping(path="/login/in", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
User loginA(@RequestBody LoginCredential newLogin)
{
logger.debug(newLogin);
return repository.findByEmailAddress(newLogin.getEMail()).get(0).getUser();
}
我试着用这种方法:
var request = new XMLHttpRequest();
let url='http://localhost:8080/login/in';
let data=JSON.stringify({ email:this.state.email,passwordHash:this.state.passwordHash});
request.open('GET', url, true);
request.setRequestHeader("Content-Type", "application/json");
request.send(data);
它给了我错误-400
斯普林说:
已解决[org.springframework.http.converter.httpMessageEndableException:缺少所需的请求正文:com.mua.cse616.Model.User com.mua.cse616.Controller.LoginCredentialController.loginA(com.mua.cse616.Model.LoginCredential)]
如何解决这个问题?我认为主要的问题是,您正在使用
@GetMapping
并同时发送body@RequestBody LoginCredential newLogin
。您应该将@RequestBody
与@PostMapping
或@PutMapping
一起使用,而不是@Getmapping
所以,尝试将您的请求更改为POST。这将解决例外情况
@PostMapping(path="/login/in", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
User loginA(@RequestBody LoginCredential newLogin)
{
logger.debug(newLogin);
return repository.findByEmailAddress(newLogin.getEMail()).get(0).getUser();
}
var request = new XMLHttpRequest();
let url='http://localhost:8080/login/in';
let data=JSON.stringify({ email:this.state.email,passwordHash:this.state.passwordHash});
request.open('POST', url, true);
request.setRequestHeader("Content-Type", "application/json");
request.send(data);