Home > Mobile >  Spring request parameter is missing
Spring request parameter is missing

Time:11-28

I'm trying to register a user by sending the username, password, firstName and lastName from the c# client to the java application using spring. In the java application, it gets the username and password but firstName and lastName get lost.

This is the error I get:

[org.springframework.web.bind.MissingServletRequestParameterException: Required request parameter 'firstName' for method parameter type String is not present]

This is the c# method I'm using to send the parameters to the java application:

public async Task<User> RegisterUserAsync(string username, string password, string firstName, string lastName)
        {
            Console.WriteLine("Registering...");
            HttpResponseMessage responseMessage =
                await Client.GetAsync($"http://localhost:8080/user/register?username={username}&password={password}&firstname={firstName}&lastname={lastName}");
            if (responseMessage.StatusCode == HttpStatusCode.OK)
            {
                string userAsJson = await responseMessage.Content.ReadAsStringAsync();
                User resultUser = JsonSerializer.Deserialize<User>(userAsJson);
                Console.WriteLine("Registered");
                return resultUser; //EXPECTED A USUAL USER TO BE RETURNED 
            }

            throw new Exception("User could not be registered");
        }

When debugging it shows that all parameters have correct values, but java does not receive them

Java application register method:

@GetMapping("/register")
    public ResponseEntity<User> ValidateRegister(@RequestParam String username, @RequestParam String password, @RequestParam String firstName, @RequestParam String lastName)
    {
        try{
            System.out.println(username);
            User user = userService.ValidateRegister(username,password,firstName,lastName);

            if(user == null) {
                return ResponseEntity.notFound().build();
            }
            return ResponseEntity.ok(user);
        }
        catch (Exception e)
        {
            System.out.println(e.getMessage());
            return ResponseEntity.badRequest().build();
        }
    }

If I put firstName and lastName as required = false, then it registers the user only with username and password.

The question is why does the username and password get through but firstName and lastName doesn't ?

CodePudding user response:

Because you do not explicitly specify the name of the parameter, which makes Spring parse the parameters by the name of the variables. And they are different for you: in С# you pass firstname and lastname, but in java your parameters are called firstName and lastName

Try it in java-code:

@RequestParam(name = "firstname") String firstName, @RequestParam(name = "lastname") String lastName

or (better) fix your C# client:

Client.GetAsync($"http://localhost:8080/user/register?username={username}&password={password}&firstName={firstName}&lastName={lastName}");

CodePudding user response:

You are using camel case for the variable name in your Java code: firstName and lastName. In the other hand the request parameter name is not using camel case: &firstname=... and &lastname. In this case I suggest explicitly specifying the name of the parameter in @RequestParam:

@RequestParam("firstname") String firstName

This is not part of your question, but please avoid sending passwords as a request parameter. This will send it as clear text, you may want to send it in the request body. Also,GET requests should be used for actions which are not modifying the state of back-end. Please use POST or PUT modifying actions, like registering users.

  • Related