Open In App

Spring Boot – Handling Url Encoded Form

Working with forms is one of the important aspects of web applications today. Form data can be submitted in various forms from which URL encoded type is the most used for secured data transfer. Through this article, we will see how we can handle URL-encoded forms in Spring Boot. So, let’s begin.

Handling of URL encoded form in Spring Boot

Example 1: Handling URL encoded form

Spring Boot handling URL encoded form using Class:

First Initialize and create a spring boot project in your IDE using spring-starter. In this article, we will configure a simple service to handle To Do list.



In the src folder where the spring application file is present create folder Todo as below.



Inside this folder we will create controller for handling URL encoded forms. Let’s add POST mapping for handling form .

@PostMapping(value="/encodedFormSubmission",consumes = "application/x-www-form-urlencoded", 
produces = "application/text")

You can see we have added consumes attribute which will accept URL encoded form data. As we are going to Form class, we will create another file which will have class for accepting form data as below.




package com.example.formhandling.Todo;
  
public class TodoForm {
    private String name;
    private String task;
  
    TodoForm(String name , String task){
        this.name = name;
        this.task = task;
    }
  
    String getname(){
        return this.name;
    }
  
    String gettask(){
        return this.task;
    }
}

The above class object will be instantiated after receiving form data. The route will look like below.




@PostMapping(value="/encodedFormSubmissionwithClass",
             consumes = "application/x-www-form-urlencoded", produces = "application/text")
public String postMethodName(TodoForm formData) {
  System.out.println(formData.getname());
  System.out.println(formData.gettask());
  return "Name : "+formData.getname()+", Task : "+formData.gettask();
}

Finally start the application and make request to server as Form URL encoded. Below is the output:

Example 2: Handling URL encoded form

Spring Boot handling URL encoded form using @RequestBody:

Now for this example we will configure above example with @RequestBody instead of class. For configuring @RequestBody we will be using MultiValueMap for accepting form data as below.




@PostMapping(value="/encodedFormSubmissionwithRequestBody",
             consumes = "application/x-www-form-urlencoded", produces = "application/text")
public String postMethodName(@RequestBody MultiValueMap<String, String> formData) {
  String name=formData.get("name").get(0);
  String task=formData.get("task").get(0);
  System.out.println(name);
  System.out.println(task);
  return "Name : "+name+", Task : "+task;
}

Now let’s make request to this route. Output depicted as below.

As you can see below if you try to submit any other type of data. you will get error “Unsupported Media Type”.

Here is complete code of controller with both example routes.




package com.example.formhandling.Todo;
  
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
  
@RestController
public class TodoController {
      
    @RequestMapping("/home")
    String todoHome(){
        return "Welcome To To Do Application";
    }
  
    @PostMapping(value="/encodedFormSubmissionwithClass",consumes = "application/x-www-form-urlencoded", produces = "application/text")
    public String postMethodName(TodoForm formData) {
        System.out.println(formData.getname());
        System.out.println(formData.gettask());
        return "Name : "+formData.getname()+", Task : "+formData.gettask();
    }
  
    @PostMapping(value="/encodedFormSubmissionwithRequestBody",consumes = "application/x-www-form-urlencoded", produces = "application/text")
    public String postMethodName(@RequestBody MultiValueMap<String, String> formData) {
        String name=formData.get("name").get(0);
        String task=formData.get("task").get(0);
        System.out.println(name);
        System.out.println(task);
        return "Name : "+name+", Task : "+task;
    }
}

Conclusion

So, that is how we handle URL encoded forms in Spring Boot. Through this article, we have seen that how we can configure spring boot to handle URL encoded forms by using class and @RequestBody. There are other methods which can be used as per requirement for handling of URL encoded forms.


Article Tags :