问题
I'm getting the following error when it tries to create an instance of 'Transacao'
`Error: Cannot construct br.com.cbmp.ecommerce.resposta.Transacao as it does not have a no-args constructor : Cannot construct br.com.cbmp.ecommerce.resposta.Transacao as it does not have a no-args constructor `
---- Debugging information ----
message : Cannot construct br.com.cbmp.ecommerce.resposta.Transacao as it does not have a no-args constructor
cause-exception : com.thoughtworks.xstream.converters.reflection.ObjectAccessException
cause-message : Cannot construct br.com.cbmp.ecommerce.resposta.Transacao as it does not have a no-args constructor `
class : br.com.cbmp.ecommerce.resposta.Transacao
required-type : br.com.cbmp.ecommerce.resposta.Transacao
path : /transacao
I am aware that there's a bug with XStream 1.3.1 and JDK7, but I'm currently using XStream 1.3.1 and JDK6. Any ideas why this error is still happening?
Thanks
回答1:
3 solutions:
- Provide a no-args constructor (obvious)
- Make this object implement Serializable.
- Upgrade to xstream 1.4.4*
I use solution #2 all the time.
*I verified 1.2.2 doesn't work and 1.4.4 does work, haven't tried any versions in-between
回答2:
Simple answer: all of your inner classes must be marked static, or you should use full e.g. not inner classes.
Long answer: Java does a few things automatically, and you've encountered an edge case (See here). It's (mostly) impossible for an inner class to have a no-args constructor. Java always adds one argument to any constructors in an inner/local class, and that argument is a reference to the parent class. This is used so you can access all variables/methods of the parent, but means that even a no-args (in code) constructor has (when using reflection, like xstream does) one argument. To fix, mark the class as static so that it no longer requires inheriting all of the parent class methods/variables.
回答3:
xstream 1.3.1 does not support anymore the deserialization under JDK 1.7 if there is no non-arg constructor.
Resolution:
1) To resolve this issue, you need to use JDK6 if you want to continue with 1.3.1 jar.
2) If you using JDK7 or later then to resolve this issue, you need to upgrade JAR xstream.jar 1.3.1 to later release.
For more detail, please refer https://jira.atlassian.com/browse/JRA-32823.
回答4:
I was able to fix a similar error message by updating to xstream 1.4.4 Now it's jdk7 friendlier. In my particular instance (even odder) it would fail in the editor, and succeed on the command line. Turns out my command line was using jdk6, editor was using jdk7. Go figure.
ref: http://xstream.10960.n7.nabble.com/JAVA-7-compatibility-problem-td7172.html
来源:https://stackoverflow.com/questions/9621372/xstream-no-args-constructor-error