java - Cannot add new user to database - Spring boot: 400 BadRequest
问题描述
I have a problem with the application. I have created 2 services, one of which (db-connector) connects to the database and allows some actions and the second (webApp) used by the browser connects to the first.
When I'm trying to create user using Postman it works. So bug must be in method below
@PostMapping("/createUser")
public String createUser(@ModelAttribute("createQuery") CreateQuery createQuery) {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
User user = new User();
user.setLogin(createQuery.getLogin());
user.setPassword(createQuery.getPassword());
user.setFirstName(createQuery.getFirstName());
user.setLastName(createQuery.getLastName());
HttpEntity<User> request = new HttpEntity<>(user, headers);
restTemplate.postForEntity("http://localhost:8080/createUser", request, User.class);
return "createUser";
}
It returns:
Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.web.client.HttpClientErrorException$BadRequest: 400 : [{"timestamp":"2020-09-10T14:57:56.859+00:00","status":400,"error":"Bad Request","message":"","path":"/createUser"}]] with root cause
org.springframework.web.client.HttpClientErrorException$BadRequest: 400 : [{"timestamp":"2020-09-10T14:57:56.859+00:00","status":400,"error":"Bad Request","message":"","path":"/createUser"}]
I've tried do it in such a way:
HttpEntity<User> request = new HttpEntity<>(new User(), headers);
restTemplate.postForEntity("http://localhost:8080/createUser?userLogin=" + createQuery.getLogin()
+ "&password=" + createQuery.getPassword() + "&firstName=" + createQuery.getFirstName()
+ "&lastName=" + createQuery.getLastName(), request, User.class);
Tried to changed postForEntity to postForObject, but still doesn't work. I can't figure out what is wrong.
Cheers :)
解决方案
[
2020-09-10 17:39:34.284 DEBUG 2116 --- [nio-8080-exec-1] o.s.jdbc.core.JdbcTemplate : Executing SQL update and returning generated keys
2020-09-10 17:39:34.285 DEBUG 2116 --- [nio-8080-exec-1] o.s.jdbc.core.JdbcTemplate : Executing prepared SQL statement [INSERT INTO EXAMPLE_SCHEMA.USERS(login, password, first_name, last_name) values(?, ?, ?, ?)]
2020-09-10 17:39:50.421 DEBUG 2116 --- [nio-8080-exec-2] o.s.jdbc.core.JdbcTemplate : Executing prepared SQL query
2020-09-10 17:39:50.421 DEBUG 2116 --- [nio-8080-exec-2] o.s.jdbc.core.JdbcTemplate : Executing prepared SQL statement [SELECT * FROM EXAMPLE_SCHEMA.USERS WHERE login LIKE ?]
2020-09-10 17:39:52.983 DEBUG 2116 --- [nio-8080-exec-3] o.s.jdbc.core.JdbcTemplate : Executing prepared SQL query
2020-09-10 17:39:52.983 DEBUG 2116 --- [nio-8080-exec-3] o.s.jdbc.core.JdbcTemplate : Executing prepared SQL statement [SELECT * FROM EXAMPLE_SCHEMA.USERS WHERE login LIKE ?]
]
目标是连接数据库是来自第一个服务的响应
推荐阅读
- json - wdio cucumber json 记者在 Jira Xray 中的错误测试结果
- matlab - 如果包含函数定义,如何从命令行运行 matlab 文件?
- python - 我正在尝试从 Ballotpedia 中刮取美国国会议员的姓名
- java - 在所有测试类中的所有测试终止后在junit 5中运行代码
- optimization - 减小 main.js Angular 11 的大小
- amazon-web-services - AWS Bitnami EC2 和 EC2 无法以 Service Unavailable 启动
- html - 欧元符号 (€) 呈现为 Û,仅在 iPhone 上
- excel - 将工作表复制到另一个工作簿并使用熊猫转换为数据透视表
- python-3.x - 不更新数据库中的天数(Python 和 sqlite)
- c++ - 如何避免符号表查找中的歧义?