DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • How To Validate HTTP Post Request Body - Restful Web Services With Spring Framework | Spring Boot
  • RESTful Web Services With Spring Boot: Reading HTTP POST Request Body
  • Spring Boot - How To Use Native SQL Queries | Restful Web Services
  • RESTful Web Services: How To Create a Context Path for Spring Boot Application or Web Service

Trending

  • Event-Driven Pipelines With Apache Pulsar and Go
  • Persistent Memory for AI Agents Using LangChain's Deep Agents
  • Pragmatica Aether: Let Java Be Java
  • Liquid Glass, Material 3, and a Lot of Plumbing
  1. DZone
  2. Coding
  3. Frameworks
  4. Build a REST Service That Consumes Any Type of Request Format

Build a REST Service That Consumes Any Type of Request Format

By 
Santosh Devkate user avatar
Santosh Devkate
·
Feb. 27, 20 · Tutorial
Likes (9)
Comment
Save
Tweet
Share
34.1K Views

Join the DZone community and get the full member experience.

Join For Free

Sometimes, we have demands from clients that require a REST service that consumes JSON/XML or any other format dynamically. In this tutorial, we're going to show you how to do exactly that with Spring Boot. 

Here is the class that I am taking as a request body in my API. Here, I am using @XmlRootElement annotation to mark an Employee class as a root. For more details about this annotation, please click here.

Java
 


xxxxxxxxxx
1
15
 
1
@XmlRootElement
2
 
          
3
public class Employee {
4
 
          
5
private int id;
6
 
          
7
private String firstName;
8
 
          
9
private String lastName;
10
 
          
11
private String department;
12
 
          
13
//setters & getters 
14
 
          
15
//toString()


You may also like: Spring Boot: The Most Notable Features You Should Know.

Here is my REST service, EmployeeController.Java.

Java
 


x
15
 
1
@RestController
2
public class EmployeeController {
3
 
          
4
    @PostMapping(path = "/get-diff-data/", consumes = MediaType.ALL_VALUE)
5
    public ResponseEntity<?> getDetails(@RequestBody Employee emp) {
6
        ResponseEntity<?> response = null;
7
        try {
8
            response = ResponseEntity.status(HttpStatus.OK).body(emp);
9
        } catch (Exception e) {
10
            response = ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage());
11
        }
12
        return response;
13
    }
14
 
          
15
}


After this, let's make sure that the service API is working properly. Here, I'll be using Postman. First, we will check with XML:

XML
 


xxxxxxxxxx
1
 
1
<?xml version="1.0" encoding="UTF-8"?>
2
<employee>
3
   <department>IT</department>
4
   <firstName>Santosh</firstName>
5
   <id>123</id>
6
   <lastName>Devkate</lastName>
7
</employee>


For the above input, this is the response we get in JSON:

JSON
 


xxxxxxxxxx
1
 
1
{
2
    "id": 123,
3
    "firstName": "Santosh",
4
    "lastName": "Devkate",
5
    "department": "IT"
6
}


Here is a snapshot of the entire service call:

XML service call

XML service call

Next, we will test it based on JSON input:

JSON
 




xxxxxxxxxx
1


 
1
{
2
    "id": 123,
3
    "firstName": "Santosh",
4
    "lastName": "Devkate",
5
    "department": "IT"
6
}



The expected response will be the same JSON:

JSON
 




x


 
1
{
2
    "id": 123,
3
    "firstName": "Santosh",
4
    "lastName": "Devkate",
5
    "department": "IT"
6
}



Thanks for reading this blog. Please feel free to post any comments on this if you have any questions!


Further Reading

  • Step-By-Step Spring Boot RESTful Web Service Complete Example.
  • XML Parsing Using Java org.w3c.dom.
  • All About Spring Boot [Tutorials and Articles].
REST Web Protocols Web Service Spring Framework Requests Build (game engine)

Opinions expressed by DZone contributors are their own.

Related

  • How To Validate HTTP Post Request Body - Restful Web Services With Spring Framework | Spring Boot
  • RESTful Web Services With Spring Boot: Reading HTTP POST Request Body
  • Spring Boot - How To Use Native SQL Queries | Restful Web Services
  • RESTful Web Services: How To Create a Context Path for Spring Boot Application or Web Service

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook