![]() |
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 formSpring 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", 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
The above class object will be instantiated after receiving form data. The route will look like below. Java
Finally start the application and make request to server as Form URL encoded. Below is the output: Example 2: Handling URL encoded formSpring 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
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
ConclusionSo, 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. |
Reffered: https://www.geeksforgeeks.org
Advance Java |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 12 |