unmarshalling via jaxb

This commit is contained in:
Francesco Mangiacrapa 2022-09-27 11:15:11 +02:00
parent f63a1becd6
commit f5cc79ec1f
5 changed files with 14 additions and 43 deletions

View File

@ -78,13 +78,6 @@
</exclusions>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>2.8.11</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.gcube.portlets.user</groupId>
<artifactId>gcube-url-shortener</artifactId>
@ -104,7 +97,6 @@
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.0</version>
<scope>provided</scope>
</dependency>
<!-- LOGGER -->

View File

@ -128,7 +128,7 @@ public class GNADataEntryConfigProfileReader {
LOG.debug("read " + PATH_TO_HEADER_GUI_PRESENTATION + ": " + currValue);
if (currValue != null && currValue.size() > 0) {
String headerGUI = currValue.get(0);
Header headerClass = SerializerUtil.readXML(headerGUI, Header.class);
Header headerClass = SerializerUtil.unmarshalXML(headerGUI, Header.class);
LOG.debug("serialized " + Header.class + ": " + headerClass);
deGUIP.setHeader(headerClass);
} else
@ -139,7 +139,7 @@ public class GNADataEntryConfigProfileReader {
LOG.debug("read " + PATH_TO_BODY_WELCOME_GUI_PRESENTATION + ": " + currValue);
if (currValue != null && currValue.size() > 0) {
String bodyWelcome = currValue.get(0);
BodyWelcome bodyWelcomeClass = SerializerUtil.readXML(bodyWelcome, BodyWelcome.class);
BodyWelcome bodyWelcomeClass = SerializerUtil.unmarshalXML(bodyWelcome, BodyWelcome.class);
LOG.debug("serialized " + BodyWelcome.class + ": " + bodyWelcomeClass);
deGUIP.setBodyWelcome(bodyWelcomeClass);
} else

View File

@ -15,7 +15,6 @@ import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
/**
@ -30,7 +29,7 @@ public class SerializerUtil {
private static Logger LOG = LoggerFactory.getLogger(SerializerUtil.class);
private static ObjectMapper mapper;
private static ObjectMapper mapperXML;
static {
@ -43,11 +42,6 @@ public class SerializerUtil {
SimpleModule s = new SimpleModule();
mapper.registerModule(s);
}
static {
mapperXML = new XmlMapper();
}
/**
* Read JSON.
@ -62,33 +56,18 @@ public class SerializerUtil {
public static <T> T readJSON(String string, Class<T> clazz) throws JsonProcessingException, IOException {
return mapper.readerFor(clazz).readValue(string);
}
/**
* Read XML.
*
* @param <T> the generic type
* @param string the string
* @param clazz the clazz
* @return the t
* @throws JsonProcessingException the json processing exception
* @throws IOException Signals that an I/O exception has occurred.
*/
public static <T> T readXML(String string, Class<T> clazz) throws JsonProcessingException, IOException {
return mapperXML.readerFor(clazz).readValue(string);
}
/**
* Unmarshal XML.
*
* @param <T> the generic type
* @param <T> the generic type
* @param string the string
* @param theClass the the class
* @return the class
* @throws Exception the exception
*/
public static <T> T unmarshalXML(String string, Class<T> theClass) throws Exception {
LOG.debug("unmarshalXML: " + string.trim());
LOG.debug("unmarshalXML: " + string.trim() + " as class: " + theClass);
try {
JAXBContext jaxbContext = JAXBContext.newInstance(theClass);
@ -98,10 +77,10 @@ public class SerializerUtil {
throw new Exception("Input string is null or empty");
InputStream stream = new ByteArrayInputStream(string.getBytes());
T umarshalledClass = (T) jaxbUnmarshaller.unmarshal(stream);
LOG.debug("returning: " + umarshalledClass);
T umarshalledObject = (T) jaxbUnmarshaller.unmarshal(stream);
LOG.debug("returning: " + umarshalledObject);
return (T) umarshalledClass;
return umarshalledObject;
} catch (Exception e) {
LOG.error("An error occurred on unmarshalling " + string + "as " + theClass, e);

View File

@ -4,7 +4,7 @@ import java.io.Serializable;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "body_welcome")
@ -13,9 +13,9 @@ public class BodyWelcome implements Serializable {
private static final long serialVersionUID = 801731912505360534L;
@XmlAttribute
@XmlElement
private String title;
@XmlAttribute
@XmlElement
private String description;
public BodyWelcome() {

View File

@ -4,7 +4,7 @@ import java.io.Serializable;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
/**
@ -20,10 +20,10 @@ public class Header implements Serializable {
private static final long serialVersionUID = 1914492109448706008L;
@XmlAttribute
@XmlElement
private String title;
@XmlAttribute
@XmlElement
private String subtitle;
/**