MarshallingHttpMessageConverter - Spring



Introduction

In Spring, marshalling and unmarshalling refer to the process of converting Java objects to a format that can be easily transmitted over the network (e.g., XML or JSON) and vice versa. Spring provides a mechanism for achieving this through the use of `HttpMessageConverter` implementations. These converters allow you to serialize and deserialize Java objects when handling HTTP requests and responses.

Here, I'll provide examples of how to use Spring's `MarshallingHttpMessageConverter` with both XML and JSON formats.

1. Configure Spring MVC for MarshallingHttpMessageConverter

First, you need to configure Spring MVC to use the `MarshallingHttpMessageConverter`. This typically involves adding it to the list of message converters in your Spring configuration.

For XML (using JAXB):

<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
    <property name="messageConverters">
        <list>
            <bean class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
                <property name="marshaller" ref="jaxb2Marshaller" />
                <property name="unmarshaller" ref="jaxb2Marshaller" />
            </bean>
        </list>
    </property>
</bean>

<bean id="jaxb2Marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
    <property name="packagesToScan">
        <list>
            <value>com.example.model</value> <!-- Package where your JAXB-annotated classes reside -->
        </list>
    </property>
</bean>

For JSON (using Jackson):

<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
    <property name="messageConverters">
        <list>
            <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" />
        </list>
    </property>
</bean>

2. Create JAXB-annotated Model Class

For XML marshalling, you need to create a model class and annotate it with JAXB annotations:

@XmlRootElement
public class Person {
    private String name;
    private int age;

    // Getters and setters
}



3. Controller for XML Marshalling

import org.springframework.web.bind.annotation.*;
import org.springframework.http.MediaType;

@RestController
@RequestMapping("/api")
public class PersonController {

    @GetMapping(value = "/person", produces = MediaType.APPLICATION_XML_VALUE)
    public Person getPerson() {
        Person person = new Person();
        person.setName("John");
        person.setAge(30);
        return person;
    }
}

4. Controller for JSON Marshalling


import org.springframework.web.bind.annotation.*;
import org.springframework.http.MediaType;

@RestController
@RequestMapping("/api")
public class PersonController {

    @GetMapping(value = "/person", produces = MediaType.APPLICATION_JSON_VALUE)
    public Person getPerson() {
        Person person = new Person();
        person.setName("John");
        person.setAge(30);
        return person;
    }
}

With these configurations and controllers in place, Spring will automatically use the `MarshallingHttpMessageConverter` (either for XML or JSON) to marshal the `Person` object when it returns from the controller method.

When you access the `/api/person` endpoint, you'll get the serialized XML or JSON response based on the content type specified in the `produces` attribute of the `@GetMapping` annotation.

Remember to include the necessary dependencies for JAXB or Jackson in your project's build configuration.


Post a Comment

Previous Post Next Post