skip to Main Content

The frontend sends data in JSON format, but the backend receives empty data.
this is the data frontend send.

{
  "user": {
    "id": 0,
    "imgUrl": "default.jpg",
    "userName": "d",
    "password": "d",
    "email": "[email protected]"
  },
  "verification": "d"
}

this is the data what received by the backend.
This is a screenshot of Debug in IDEA, the user and verification in the figure are expected to get data from the frontend, but Debug finds that their values are null.
The User class is defined as follows

package com.example.back.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
    private String userName;
    private String email;
    private String password;
    private Integer id;
    private String imgUrl;
    public User(String name,String password,String email){
        this.userName=name;
        this.password=password;
        this.email=email;
    }
}

Next is the code in which the problem occurred.

@PostMapping("/signin")
public Message signIn( User user, String verification) {
     if (Boolean.TRUE.equals(redisTemplate.hasKey(user.getEmail())) && Boolean.TRUE.equals(redisTemplate.hasKey(verification))) {
         if (redisTemplate.opsForValue().get(user.getEmail()) == verification) {
             try {
                 userMapper.insert(user);
             } catch (DuplicateKeyException e) {
                 return Message.error("用户已注册");
             }
             user = userMapper.getByEmail(user.getEmail());
             String token = JWTUtils.generToken(user.getId());
             return Message.success("注册成功", token);
         }
     }
     return Message.error("验证码错误");
 }

I’ve tried adding @RequestParam to the two parameters separately to fix this, but without success.

2

Answers


  1. Maybe because use must ctach the params like this:
    public Message signIn(@RequestBody User user, @RequestParam String verification)

    the @RequestBody and @RequestParam notations are missing

    Login or Signup to reply.
  2. You are missing @RequestBody. Also in the json user and verification are part of another object – the json does not match what you parse it to in the backend.

    public class SignIn {
    
      private User user;
      private String verification;
    
      //getters and setters
    }
    

    In the handler method:

    @PostMapping("/signin")
    public Message signIn(@RequestBody SignIn signIn) {
      ...
    }
    

    also don’t forget you should use @RestController or
    @Controller @ResponseBody

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search