I'm creating a spring boot rest API which should accept xml.
Once i get the data to be accepted by the controller i can jiggle my way forward.
So basically my question is, how do i get the controller to accept the data?
My understanding is that i can use either jaxb or jackson for this, and jackson is to be preferred(?)
The controller will look something like
package com.example.rest;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/myapi")
public class myController {
@RequestMapping(method = RequestMethod.POST, path = "/xmlentry", consumes = "application/xml")
public void doStuff() {// do stuff }
}
The input i get (which i feed into postman) is
<Game>
<numberOfBalls>8</numberOfBalls>
<players>
<human>
<id>1001</id>
<name>John</name>
<skill>40</skill>
</human>
<human>
<id>2001</id>
<name>Jake</name>
<skill>58</skill>
</human>
<human>
<id>3001</id>
<name>Jane</name>
<skill>50</skill>
</human>
</players>
<bonus>
<round nr="1">
<id number="1001">1</id>
<id number="2001">1</id>
<id number="3001">4</id>
</round>
<round nr="2">
<id number="1001">6</id>
<id number="2001">0</id>
<id number="3001">1</id>
</round>
</bonus>
</Game>
So, my instinct is to add
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
</dependency>
to the pom file and set doStuff() as
public void doStuff(@RequestBody Game game) {}
where i create a pojo Game class (containing numberOfBalls (int), players (list of humans) , bonus (list of rounds)) and then creating humans etc.
Is this the easy way of doing it? A little confused here.
Appreciate any help.