内部服务器错误的Post请求Spring Boot [Java]

huangapple go评论85阅读模式
英文:

Internal Server Error on Post Request Spring Boot [Java]

问题

抱歉,我无法完成您的请求。

英文:

I am trying to send a post request to add an User object to the list of users (no database).
getUser, getAllUsers Api endpoints are working fine and giving desired results.

When I send a Post request with User, it is always giving 500 Internal Server Error.

I have also printed out the User and data is fine.

Below is my code -

User Entity (User.java) -

  1. public class User {
  2. private Integer userId;
  3. private String name;
  4. private String email;
  5. private Account account;

Getters and Setters along with constructors are also there.

My User Service and its implementation -

UserService.java -

  1. @Service
  2. public interface UserService {
  3. public User getUser(Integer userId);
  4. public List<User> getAllUsers();
  5. <T> ResponseEntity<T> deleteUser(Integer userId);
  6. <T> ResponseEntity<User> addUser(User user);
  7. }

UserServiceImpl.java

  1. @Service
  2. public class UserServiceImpl implements UserService{
  3. List<User> userList = List.of(
  4. new User(1311, "Vaibhav Shrivastava", "innomightmail@gmail.com"),
  5. new User(1312, "Varun Shrivastava", "varun@gmail.com"),
  6. new User(1313, "Rajesh Shrivastava", "rajesh@gmail.com")
  7. );
  8. @Override
  9. public User getUser(Integer id) {
  10. // TODO Auto-generated method stub
  11. System.out.println(this.userList.stream().filter(user -> user.getUserId().equals(id)).findAny().orElse(null));
  12. return this.userList.stream().filter(user -> user.getUserId().equals(id)).findAny().orElse(null);
  13. }
  14. @Override
  15. public List<User> getAllUsers() {
  16. // TODO Auto-generated method stub
  17. return userList;
  18. }
  19. @Override
  20. public <T> ResponseEntity<User> addUser(User user) {
  21. // TODO Auto-generated method stub
  22. userList.add(user);
  23. return new ResponseEntity<User>((User)user, HttpStatus.OK);
  24. }

Controller (UserController.java) -

  1. @RestController
  2. @RequestMapping("/user")
  3. public class UserController {
  4. @Autowired
  5. private UserService userService;
  6. @Autowired
  7. private RestTemplate restTemplate;
  8. @GetMapping("/{userId}")
  9. public User getUser(@PathVariable("userId") Integer userId) {
  10. User user = this.userService.getUser(userId);
  11. Account account = this.restTemplate.getForObject("http://account-service/account/user/" + user.getUserId(), Account.class);
  12. user.setAccount(account);
  13. return user;
  14. }
  15. @GetMapping("/all-users")
  16. public List<User> getAllUsers(){
  17. return this.userService.getAllUsers();
  18. }
  19. @PostMapping("/add-user")
  20. public ResponseEntity<Object> addUser(@RequestBody User user) {
  21. try {
  22. User newUser = new User(user.getUserId(), user.getName(), user.getEmail());
  23. this.userService.addUser(newUser);
  24. return new ResponseEntity<>(user, HttpStatus.CREATED);
  25. } catch (Exception e) {
  26. User newUser = new User(user.getUserId(), user.getName(), user.getEmail());
  27. return new ResponseEntity<>(newUser, HttpStatus.INTERNAL_SERVER_ERROR);
  28. }
  29. }

getUser and getAllUsers are working fine (dont get confused by account in getUser that is my another microservice)

When I am sending Post request using postman -

  1. {
  2. "userId": 1314,
  3. "name": "James",
  4. "email": "james@gmail.com"
  5. }

I am getting response -
500 Internal server error and User object which I wanted to check so I printed it out -

  1. {
  2. "userId": 1314,
  3. "name": "Kavita Shrivastava",
  4. "email": "kavita@gmail.com",
  5. "account": null
  6. }

I do not know why I am getting Internal server error.

The request should add the new User to the list in UserServiceImpl.

答案1

得分: 2

堆栈跟踪包含您正在寻找的所有信息。 UserServiceImple.addUser 尝试向 userList 添加一个对象。但 userList 是一个不可变列表,所以它引发异常。代码流程到 "catch" 块,然后返回 500,同时返回新创建的对象。

英文:

Stack trace has all the information you are looking for. UserServiceImple.addUser tries to add an object to the userList . but userList is an immutable list. so it throws exception. code flows to "catch" block and it returns 500 with newly created object.

答案2

得分: 1

根据我的看法,这是因为当你使用List.of时,它等同于Collections.unmodifiableList,因此如果尝试修改它,会引发异常,因为它是不可变的。

英文:

In my opinion That's happens because
when U use List.of it equals to
Collections.unmodifiableList so it will throw an exception as Its Immutable.

huangapple
  • 本文由 发表于 2023年2月16日 18:09:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/75470694.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定