There will be some scenarios, where you need to send both the XML and JSON output based on the calling client requests. This might become sometimes tricky if we are using the Message Converters other than Jackson provides the ability to generate both the XML and JSON.
Follow the below steps:
Step:1 Maven dependency.
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>2.10.0</version>
</dependency>
Add the above dependency, once this is available the message convertor Jackson will be available.
Stpe:2 Define the outputs in the Controller.
@RestController
public class HelloController {
@RequestMapping(value = "/greet", consumes = MediaType.APPLICATION_JSON_VALUE, produces = {MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE })
public GreetingPojo index() {
MessagePojo msg = new MessagePojo();
msg.setMessage("Greetings from Spring Boot!");
// GreetingPojo targetObj = new DozerBeanMapper().map(msg, GreetingPojo.class);
GreetingPojo targetObj = GreetMapper.INSTANCE.msgtoGreet(msg);
return targetObj;
}
}
Stpe:3 Request for Json/Xml
The only Constraint is we need to pass the object from the controller as it. The Jackson takes everything.
Happy Learning !!!!
No comments:
Write comments