Spring MVC HTTP Message Converters Implementation

Spring MVC provides powerful mechanisms for handling HTTP message conversion through @ResponseBody and @RequestBody annotations. These annotations enable seamless transformation between Java objects and HTTP representations.

HTTP message converters facilitate direct conversion of controller-generated data into client-friend formats. The framework automatically selects appropriate converters based on request headers and available libraries.

When working with JSON data, Spring can utilize either MappingJacksonHttpMessageConverter or MappingJackson2HttpMessageConverter, depending on the Jackson library version present in the classpath.

The produces attribute handles requests where the client's Accept header indicates preference for application/json responses, provided Jackson JSON is available. The converter automatical transforms controller return values into JSON documents.

The consumes attribute processes requests with ContentType header set to application/json, again requiring Jackson JSON availabliity. In this case, the converter transforms incoming JSON documents into Java objects for controller consumption.

Using @RestController annotation enables automatic message conversion for all handler methods within the controller class.

The @RequestBody annotation triggers the lookup of an appropriate message converter to transform client resource representations into Java objects.

@RestController
public class DataController {
    
    @RequestMapping(value="/employee", 
                   consumes="application/json", 
                   produces="application/json")
    @ResponseBody
    public Map<String, String> handleEmployee(@RequestBody Employee employee) {
        System.out.println(employee);
        Map<String, String> response = new HashMap<>();
        response.put("result", "success");
        return response;
    }
}

public class Employee {
    private String fullName;
    private String gender;
    
    public String getFullName() {
        return fullName;
    }
    
    public void setFullName(String fullName) {
        this.fullName = fullName;
    }
    
    public String getGender() {
        return gender;
    }
    
    public void setGender(String gender) {
        this.gender = gender;
    }
}


<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>API Test Client</title>
    <script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/vue-resource@1.5.1/dist/vue-resource.min.js"></script>
</head>
<body>
    <div id="clientApp">
        <button @click="sendData()">Send Request</button>
    </div>
    
    <script>
    Vue.http.options.emulateJSON = true;
    
    new Vue({
        el: "#clientApp",
        methods: {
            sendData: function() {
                var employeeData = {
                    fullName: 'john doe',
                    gender: 'male'
                };
                
                this.$http.post('http://localhost:8080/webapp/employee', 
                               employeeData,
                               { emulateJSON: false })
                    .then(function(response) {
                        alert(JSON.stringify(response.body));
                    }, function(error) {
                        console.error(error);
                    });
            }
        }
    });
    </script>
</body>
</html>

Tags: Spring MVC HTTP Message Converters REST API jackson JSON Processing

Posted on Tue, 07 Jul 2026 17:09:49 +0000 by Calamity-Clare