I'm wondering how, calling an external API service to get a list of object mapped by my class.
This is what the XML looks like
<ArrayOfObject>
<Object>
<id></id>
<name></name>
<var1></var1>
<var2></var2>
<var3></var3>
</Object>
<Object>
<id></id>
<name></name>
<var1></var1>
<var2></var2>
<var3></var3>
</Object>
...
</ArrayOfObject>
I'm trying to get only the id and name so i've tried to map in my class only theese two fields with @JsonIgnoreProperties
annotation:
@JsonIgnoreProperties
public class Object{
public String id, name;
public Object() {}
public Object(String id, String name) {
this.id = id;
this.name= name;
}
public String getId() {return id;}
public void setId(String id) {
this.id = id;
}
public String getName() {return name;}
public void setName(String name) {
this.name = name;
}
and wrap it into
public class ArrayOfObject {
@JacksonXmlElementWrapper(useWrapping = false)
private List<Object> object;
public ArrayOfObject () {
object = new ArrayList<>();
}
public List<Object> getObjects() {return objects;}
public void setObjects(List<Object> objects) {
this.object = objects;
}
}
So now this is what my controller looks like
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
@RequestMapping
public class RestCall {
@GetMapping("/objects")
public List<Object> getObject(){
final String uri = "";
RestTemplate rt = new RestTemplate();
ArrayOfObject result = rt.getForObject(uri, ArrayOfObject.class);
return result.getObjects();
}
}
that's my pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>it.me</groupId>
<artifactId>Reading-test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Reading-test</name>
<description>Service</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
I'm wondering why it gives back an empty list, am i missing something in the serialization or could be something else?
It would be easier to use a for loop and adding those object to a list? If it would how can i implement something like that?
This is part of a previous question but not a duplicate.
CodePudding user response:
Don't know if anybody cares but i've solved adding a few annotations and now looks like:
package it.me.model;
import java.io.Serializable;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
@JacksonXmlRootElement(localName = "Object")
public class Object implements Serializable{
private static final long serialVersionUID = 1L;
@JacksonXmlProperty
private String id;
@JacksonXmlProperty
private String name;
@JacksonXmlProperty
private String var1;
@JacksonXmlProperty
private String var2;
@JacksonXmlProperty
private String var3;
public Object() {}
public Object(String id, String name, String var1, String var2, String var3) {
this.id = id;
this.name = name;
this.var1= var1;
this.var2= var2;
this.var3= var3;
}
//getters n setters
public static long getSerialversionuid() {return serialVersionUID;}
//tostring, equals, hashcode overrides
}
That's the wrapper class:
package it.me.model;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
@JacksonXmlRootElement
public class ArrayOfObject implements Serializable{
private static final long serialVersionUID = 1L;
@JacksonXmlProperty(localName = "Object")
@JacksonXmlElementWrapper(useWrapping = false)
private List<Object> ArrayOfObject = new ArrayList<>();
public ArrayOfObject() {}
public ArrayOfObject(List<Object> arrayOfObject) {
ArrayOfObject = arrayOfObject;
}
public List<Object> getArrayOfObject() {return ArrayOfObject;}
public void setArrayOfObject(List<Object> arrayOfObject) {
ArrayOfObject = arrayOfObject;
}
public static long getSerialversionuid() {return serialVersionUID;}
}
In needed in my pom.xml
thoose: (not sure about the 2nd one)
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
</dependency>
Finally that's the controller:
package it.me.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import it.me.model.ArrayOfObject;
@RestController
@RequestMapping
public class Controller {
@GetMapping("/objects")
public ArrayOfObject getObjects(){
final String uri = ""
RestTemplate rt = new RestTemplate();
ArrayOfObject result = rt.getForObject(uri, ArrayOfObject.class);
return result;
}
}