首页 > 解决方案 > 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 :)

标签: javaspring

解决方案


[

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 ?]

]

在此处输入图像描述

目标是连接数据库是来自第一个服务的响应


推荐阅读