Handling of URL encoded form in Spring Boot

  • In Spring Boot, we use Post mapping for submission of Forms. Post mapping handles HTTP POST requests.
  • Post mapping in spring boot can be configured to accept URL-encoded forms.
  • We can use the consumes attribute to set the acceptable data type for the route. We can set it to application/x-www-form-urlencoded or {MediaType.APPLICATION_FORM_URLENCODED_VALUE}.
  • By making the above changes we can handle URL-encoded forms 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.

Java




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.

Java




@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.

Java




@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.

Java




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


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.

  • Spring Boot: Spring Boot is a Java framework built over Spring framework which helps in the development of production-grade, microservice-based web applications, and APIs.
  • Http Request: Http Request helps to transfer data between frontend and backend servers.

Similar Reads

Handling of URL encoded form in Spring Boot

In Spring Boot, we use Post mapping for submission of Forms. Post mapping handles HTTP POST requests. Post mapping in spring boot can be configured to accept URL-encoded forms. We can use the consumes attribute to set the acceptable data type for the route. We can set it to application/x-www-form-urlencoded or {MediaType.APPLICATION_FORM_URLENCODED_VALUE}. By making the above changes we can handle URL-encoded forms in Spring boot....

Conclusion

...

Contact Us