neelrev / resting

Lightweight Java component to consume REST service and transform response into objects.
0 stars 0 forks source link

Empty number field to Long, causing error. #11

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
How do I set a empty number field.. The XML returned has a empty parentId. 
because of that I get a conversion error, see below.

XMLAlias alias=new XMLAlias().add("standard", Standard.class).add("standards", 
ArrayList.class); 

List resultset = 
Resting.getByXML("http://192.168.0.4/standards.xml?auth_token=1234", 
3000,null,ArrayList.class, alias);

<?xml version="1.0" encoding="UTF-8"?>
<standards>
  <standard>
    <id>1</id>
    <title>Safety</title>
    <parentId></parentId>
    <parentTitle></parentTitle>
    <level>0</level>
    <noOfChildren>6</noOfChildren>
  </standard>
</standards>

public class Standard {
    Long id;
    Long lft;
    Long rgt;
    Long parentId;
    String title;
    Date createdAt;
    Date updatedAt;
...

com.thoughtworks.xstream.converters.ConversionException: Invalid long: "" : 
Invalid long: ""
---- Debugging information ----
message             : Invalid long: ""
cause-exception     : java.lang.NumberFormatException
cause-message       : Invalid long: ""
class               : java.util.ArrayList
required-type       : java.lang.Long
path                : /standards/standard/parentId
-------------------------------
at 
com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:89)
at 
com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractRefe
renceUnmarshaller.java:63)
at 
com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.j
ava:76)
at 
com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmar
shallField(AbstractReflectionConverter.java:246)
at 
com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnm
arshal(AbstractReflectionConverter.java:218)
at 
com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmar
shal(AbstractReflectionConverter.java:162)
at 
com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:82)
at 
com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractRefe
renceUnmarshaller.java:63)
at 
com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.j
ava:76)
at 
com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.j
ava:60)
at 
com.thoughtworks.xstream.converters.collections.AbstractCollectionConverter.read
Item(AbstractCollectionConverter.java:71)
at 
com.thoughtworks.xstream.converters.collections.CollectionConverter.populateColl
ection(CollectionConverter.java:68)
at 
com.thoughtworks.xstream.converters.collections.CollectionConverter.unmarshal(Co
llectionConverter.java:61)
at 
com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:82)
at 
com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractRefe
renceUnmarshaller.java:63)
at 
com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.j
ava:76)
at 
com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.j
ava:60)
at 
com.thoughtworks.xstream.core.TreeUnmarshaller.start(TreeUnmarshaller.java:137)
at 
com.thoughtworks.xstream.core.AbstractTreeMarshallingStrategy.unmarshal(Abstract
TreeMarshallingStrategy.java:33)
at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:923)
at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:909)
at com.thoughtworks.xstream.XStream.fromXML(XStream.java:853)
at com.thoughtworks.xstream.XStream.fromXML(XStream.java:845)
at 
com.google.resting.transform.impl.XMLTransformer.createEntity(XMLTransformer.jav
a:44)
at 
com.google.resting.transform.impl.XMLTransformer.getEntityList(XMLTransformer.ja
va:56)
at 
com.google.resting.helper.RestingHelper.executeAndTransform(RestingHelper.java:6
9)
at com.google.resting.Resting.getByXML(Resting.java:320)
at 
com.meratech.ods.service.StandardsWebservice.getStandards(StandardsWebservice.ja
va:35)
at com.meratech.ods.StandardsActivity.listStandards(StandardsActivity.java:50)
at 
com.meratech.ods.test.StandardsWebserviceTest.testGetStandardReturnFullList(Stan
dardsWebserviceTest.java:23)
at java.lang.reflect.Method.invokeNative(Native Method)
at 
android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:214)
at 
android.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:199)
at 
android.test.ActivityInstrumentationTestCase2.runTest(ActivityInstrumentationTes
tCase2.java:186)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154)
at 
android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:52
5)
at 
android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1547)
Caused by: java.lang.NumberFormatException: Invalid long: ""
at java.lang.Long.invalidLong(Long.java:116)
at java.lang.Long.decode(Long.java:134)
at 
com.thoughtworks.xstream.converters.basic.LongConverter.fromString(LongConverter
.java:27)
at 
com.thoughtworks.xstream.converters.SingleValueConverterWrapper.fromString(Singl
eValueConverterWrapper.java:41)
at 
com.thoughtworks.xstream.converters.SingleValueConverterWrapper.unmarshal(Single
ValueConverterWrapper.java:49)
at 
com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:82)
... 43 more

Original issue reported on code.google.com by rmahnove...@gmail.com on 6 Jun 2011 at 11:35

GoogleCodeExporter commented 9 years ago
Resting internally uses XStream v1.3 to handle XML parsing which is throwing 
this exception. Please go through the documentation available at 
http://xstream.codehaus.org/. You may need to write a custom converter and 
register it with XMLAlias using addConverter() method. The link below may be 
useful:

http://comments.gmane.org/gmane.comp.java.xstream.user/7401

Original comment by sujata...@gmail.com on 9 Jun 2011 at 7:40

GoogleCodeExporter commented 9 years ago

Original comment by sujata...@gmail.com on 9 Jun 2011 at 7:44

GoogleCodeExporter commented 9 years ago
A quick workaround will be to read the xml data as Strings, then convert into 
Long/other objects. In that case, the xstream converter will not be reqd. 

public class Standard {
    String id;
    String lft;
    String rgt;
    String parentId;
    String title;
....
This should not be the long term solution though, in which case there should be 
a reflection converter in place.

Original comment by sujata...@gmail.com on 9 Jun 2011 at 10:19