Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Spring request parameter is missing

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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 ?

>Solution :

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}");

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading