JAXB XML parsing returns empty list for generic list - java

When attempting to parse XML with JAXBContext.newInstance(GetVehiclesResponse.class) I get APIResponse [status=0, response=Response [items=[]]] (toString not shown here). Have tried mixing/changing annotations but probably haven't found the right combination.
#XmlRootElement(name="nodes")
public class GetVehiclesResponse extends APIResponse<Vehicle> {
}
.
#XmlAccessorType(XmlAccessType.FIELD)
public class APIResponse<T> {
private String status;
private Response<T> response;
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public Response<T> getResponse() {
return response;
}
public void setResponse(Response<T> response) {
this.response = response;
}
}
.
#XmlRootElement
public class Response<T> {
private List<T> items;
public Response() {
items = new ArrayList<T>();
}
public Response(List<T> items) {
this.items = items;
}
#XmlElementWrapper
public List<T> getItems() {
return items;
}
}
.
#XmlRootElement(name="node")
#XmlAccessorType(XmlAccessType.FIELD)
public class Vehicle {
private String id;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
XML
<nodes>
<status>0</status>
<response>
<node key="0">
<id>...</id>
<name>...</name>
</node>
<node key="1">
<id>...</id>
<name>...</name>
</node>
</response>
</nodes>
Some text to make this post valid. Some text to make this post valid. Some text to make this post valid.

Related

xml response format in dropwizard

I want both xml and json format for single api.
My problem is in xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Fruits>
<response>
<name>Mango</name>
<qty>2kg</qty>
<rate>60.00</rate>
</response>
<response>
<name>Banana</name>
<qty>2kg</qty>
<rate>80.00</rate>
</response>
</Fruits>
am getting response like this but i want response like
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<response>
<list>
<item>
<name>Mango</name>
<qty>2kg</qty>
<rate>60.00</rate>
</item>
</list>
</response>
POJO is
#XmlRootElement(name ="response")
public class Fruit {
private String name;
private String qty;
private String rate;
public GetSellerBuyerCode(String name, String qty, String rate) {
this.name=name;
this.qty=qty;
this.rate=rate;
}
#XmlElement
public String getName() {
return name;
}
#XmlElement
public String getQty() {
return qty;
}
#XmlElement
public String getRate() {
return rate;
}
}
Controller Code :
#Path("/Fruits")
#POST
#Produces({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML})
public Response getSellersBySellerCodeDet() {
List<Fruit> list = new ArrayList<Fruit>();
list = buyDAO.getFruits();
return Response.ok(new GenericEntity<List<Fruit>>(list) {}).build();
}
I tried #XmlRootElement but class name is coming as root element. can any one tell how to avoid this. and am using dropwizard 1.1.2 and jersey 2.25.1
Below solution worked for me, not tried with dropwizard, only tried with Jaxb.
Can you please check ?
#XmlRootElement(name="Response")
#XmlAccessorType(XmlAccessType.FIELD)
class FruitList{
#XMLElement(name="item")
private ArrayList<Fruit> list;
public ArrayList<Fruit> getList() {
return list;
}
public void setList(ArrayList<Fruit> list) {
this.list = list;
}
}
public class Fruit {
#XmlElement(name="name")
private String name;
#XmlElement(name="qty")
private String qty;
#XmlElement(name="rate")
private String rate;
public Fruit () {
}
public Fruit (String name, String qty, String rate) {
this.name=name;
this.qty=qty;
this.rate=rate;
}
public String getName() {
return name;
}
public String getQty() {
return qty;
}
public String getRate() {
return rate;
}
}

JAXB Unmarshalling a List of different types

I have the following xml:
<scope>
<heading>
<code></code>
</heading>
<heading>
<code></code>
</heading>
<value>
<code></code>
</value>
<value>
<code></code>
</value>
</scope>
How would I go about unmarshalling this so that I had two lists.
Thanks.
#XmlRootElement
#XmlAccessorType(XmlAccessType.FIELD)
public static class Scope {
#XmlElement(name = "heading")
List<Heading> headings = new LinkedList<>();
#XmlElement(name = "value")
List<Value> values = new LinkedList<>();
public Scope() {
}
public List<Heading> getHeadings() {
return headings;
}
public void setHeadings(List<Heading> headings) {
this.headings = headings;
}
public List<Value> getValues() {
return values;
}
public void setValues(List<Value> values) {
this.values = values;
}
}
#XmlRootElement
#XmlAccessorType(XmlAccessType.FIELD)
public static class Heading {
private String code;
public Heading() {
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
}
#XmlRootElement
#XmlAccessorType(XmlAccessType.FIELD)
public static class Value {
private String code;
public Value() {
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
}

Stop converting < to &lt, > to &gt and & to & in spring controller response

I am trying to generate a small xml. This xml should have a CDATA in it. Now the problem is I managed to generate CDATA, but the xml that is generated have < and > and &
I am using spring-mvc. I am interested in getting the right xml with CDATA in it.
Sample xml is as follow :
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Customer>
<Active>1</Active>
<AlertMessage></AlertMessage>
<Currency>GBP</Currency>
<CustomerNumber>123</CustomerNumber>
<Forename>AD</Forename>
<balancetoallow>
<balance saving="10" spend="9990" type="2">
<value>\<![CDATA[$100 off on two purchase of jeans XYZ Brand.]]\></value>
</balance>
</balancetoallow>
<Surname>CD</Surname>
</Customer>
I have also overridden one of the converter as follow.
<mvc:annotation-driven>
<mvc:message-converters>
<bean
class="org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter">
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
P.S I am using spring rest controller for my web service.
Here is the model class :
public class Balance{
private String value;
private String type;
private String saving;
private String spend;
public String getValue() {
return value;
}
#XmlJavaTypeAdapter(AdapterCDATA.class)
public void setValue(String value) {
this.value = value;
}
public String getType() {
return type;
}
#XmlAttribute
public void setType(String type) {
this.type = type;
}
public String getSaving() {
return saving;
}
#XmlAttribute
public void setSaving(String saving) {
this.saving = saving;
}
public String getSpend() {
return spend;
}
#XmlAttribute
public void setSpend(String spend) {
this.spend = spend;
}
}
This class is again part of another class.
public class Balancetoallows {
private List<Balance> balancetoallow ;
public List<Balance> getBalancetoallow () {
return balancetoallow;
}
#XmlElement(name="balancetoallow ")
public void setBalancetoallow (List<Balance > balancetoallow ) {
this.balancetoallow= balancetoallow;
}
}
And this is part of this class.
The parent class.
#XmlRootElement(name = "Customer")
public class Customer {
private String surname ;
private String forename;
private BalanceToAllow balanceToAllow;
private final String active="1";
private final String currency = "GBP";
private final String alertMessage="";
public String getCustomerNumber() {
return customerNumber;
}
#XmlElement(name="CustomerNumber")
public void setCustomerNumber(String customerNumber) {
this.customerNumber = customerNumber;
}
#XmlElement(name="Active")
public String getActive() {
return active;
}
#XmlElement(name="Currency")
public String getCurrency() {
return currency;
}
public String getSurname() {
return surname;
}
#XmlElement(name="Surname")
public void setSurname(String surname) {
this.surname = surname;
}
public String getForename() {
return forename;
}
#XmlElement(name="Forename")
public void setForename(String forename) {
this.forename = forename;
}
#XmlElement(name="AlertMessage")
public String getAlertMessage() {
return alertMessage;
}
public BalanceToAllow getBalanceToAllow() {
return BalanceToAllow;
}
#XmlElement(name="balanceToAllow")
public void setFuturePromotions(BalanceToAllow balanceToAllow) {
this.balanceToAllow= balanceToAllow;
}
}
Thanks for reading.

Parsing custom tags using Simple XML Serialization

I have the below XML :
<com.abc.xyz.container>
<data class="list">
<com.xyz.abc.VO.fruits>
<id>28</id>
<name>
<![CDATA[ Apple ]]>
</name>
</<com.xyz.abc.VO.fruits>
<com.xyz.abc.VO.fruits>
<id>29</id>
<name>
<![CDATA[ Orange ]]>
</name>
</<com.xyz.abc.VO.fruits>
<com.abc.xyz.container>
I tried to create class in this manner :
#Root(name = "com.abc.xyz.container")
public class Response {
#Element(required = false)
private Data list;
public Data getList() {
return list;
}
public void setList(Data list) {
this.list = list;
}
}
public class Data {
#ElementList(inline = true, name = "com.xyz.abc.VO.fruits", required = false)
private List<Fruits> fruits;
public List<Fruits> getfruits() {
return fruits;
}
public void setfruits(List<Fruits> fruits) {
this.fruits = fruits;
}
}
EDIT 1
public class Fruits{
#Element(required = false)
private int id;
#Element(required = false)
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
But when i try to get the size of the List<Fruits> it gives me NullPointerException.
How can i correct my code ? I am using Simple XML Serialization(org.simpleframework.xml.*).

Jackson XML Parsing

Jackson 2.2.3
First, please excuse the stupid mistakes, I'm on a disconnected network, so I had to retype manually)
I have the following XML:
<orgs>
<org name="Test1">
<item>a</item>
<item>b</item>
</org>
<org name="Test2">
<item>c</item>
<item>d</item>
<item>e</item>
</org>
</orgs>
I have the following class to parse this:
#XmlRootElement(name = "orgs")
#XmlAccessorType(XmlAccessType.FIELD)
public class XmlOrgElements {
private List<Org> orgs;
public List<Org> getOrgs() {
return orgs;
}
public void setOrg(List<Org> orgs) {
this.orgs = orgs;
}
public class Org {
#JacksonXmlProperty(isAttribute = true)
private String name;
private List<Item> items;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Item> getItems() {
return items;
}
public void setName(List<Item> items) {
this.items = items;
}
}
public class Item {
#JacksonXmlText
private String item;
public String getItem() {
return item;
}
public void setItem(String item) {
this.item = item;
}
}
}
But all I'm getting back is "orgs=null". Does anyone know why?
You need to enable unwrapped handling for lists; default is to use "wrapped" format. The best way to diagnose this problem is to start with Java objects, serialize as XML, and see what the output format is.
This gives an idea of how structure differs.
If you want to default to unwrapped style, you can use:
JacksonXmlModule module = new JacksonXmlModule();
module.setDefaultUseWrapper(false);
mapper.registerModule(module);
There is also an annotation #JacksonXmlElementWrapper:
public class Bean {
#JacksonXmlElementWrapper(useWrapping=false)
public List<Stuff> entry;
}
to change behavior on per-list-property basis.
Here is the answer for those reading along:
#JacksonXmlRootElement(localname = "orgs")
public class Orgs {
#JacksonXmlElementWrapper(useWrapping = false)
private List<Org> org;
public List<Org> getOrg() {
return org;
}
public void setOrg(List<Org> org) {
this.orgs = org;
}
public Orgs() {}
}
public class Org {
#JacksonXmlProperty(isAttribute = true)
private String name;
#JacksonXmlElementWrapper(useWrapping = false)
private List<String> item;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<String> getItem() {
return item;
}
public void setItem(List<String> item) {
this.item = item;
}
}

Resources