2

I've been using a tutorial to get my object exported as XML... I'm seeing the below error with the below object. I'm not sure what's going wrong.... first I got errors about the id and name elements that are commented out, then an error about the root element.

    Mapped exception to response: 500 (Internal Server Error) javax.ws.rs.WebApplicationException: 
   com.sun.xml.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions java.sql.Date does not have a no-arg default constructor.
this problem is related to the following location: at java.sql.Date at private java.sql.Date contentmanagement.Guest.signingDate at contentmanagement.Guest at com.sun.jersey.core.provider.jaxb.AbstractRootElementProvider.writeTo(AbstractRootElementProvider.java:159) at com.sun.jersey.spi.container.ContainerResponse.write(ContainerResponse.java:306) at.....

IT seems like my date Your feedback would be appreciated!

package contentmanagement;

import java.io.Serializable;
import java.sql.Date;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@Entity
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Guest implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String name;
    private Date signingDate;

    public Guest() {
    }

    public Guest(String name) {
        this.name = name;
        this.signingDate = new Date(System.currentTimeMillis());
    }

    //@XmlElement(name = "id")
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    //@XmlElement(name = "name")
    public String getName() {
        return name;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Guest)) {
            return false;
        }
        Guest other = (Guest) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return name + " (signed on " + signingDate + ")";
    }

}

Side note: is there any better way to view logs? than in a solid string showing a stack trace? Any special apps or anything? Currently I'm viewing my logs at: http://localhost:4848/common/index.jsf

The controller throwing the error is:

@GET @Path("getGuestsXml")
@Produces("application/xml")
public Guest getGuestsXml() {
    Guest response = new Guest();

    //response.setId(9L);

    return response;
}
Ben
  • 769
  • 2
  • 7
  • 17

1 Answers1

1

Looks like java.sql.Date is the trouble maker as it does not have a zero-arg constructor. You may have to program a serializer/adapter for it.

Find more here, hoping it helps:

jaxb unmarshal timestamp

How to serialize java.sql.Date as Long, when using web services?

As far as logs/stacktraces are concerned: What other view would you prefer? Seen from a developer's perspective, verbose stacktraces are nice. For users of your system, you never want to show them, so make sure you catch these and show a gentle error page instead.

Community
  • 1
  • 1
mkro
  • 1,902
  • 14
  • 15
  • I'm a developer, so I'd like a way to view my log files to help me find problems the fastest, no matter the learning curve. – Ben Dec 14 '11 at 01:07
  • I don't understand why it's saying it doesn't have a zero argument constructor. If I pass a value to the `Guest` constructor then it should execute my 2nd constructor which means `new Date(System.currentTimeMillis());` has a value being passed to the constructor, but it still fails. Does defining the `signingDate` property by default execute the no argument constructor? – Ben Dec 14 '11 at 01:09
  • That's a fair comment. I think the fastest way to spot problems is to learn to read stacktraces (top to bottom, or bottom to top once you have more experience). When you deal with files (or huge files), grep is a great help. But first you need to understand that one line in the stacktrace that tells you about the root problem. What I wrote above is based on your stacktrace which complains about the fact that sql.Date does not have a zero-arg constructor. That's verbose and at the same time directing towards a solution quickly. – mkro Dec 14 '11 at 01:11
  • No, it does not work like this and we could now argue a lot about whether the design of JAXB is good or bad. However: The binding framework is looking for a zero-arg constructor. That is the root cause of the exception you're seeing. Please check the links and feel free to ask again, if they don't help. – mkro Dec 14 '11 at 01:12
  • Are there any tools to help read the stacktraces is a formatted way? – Ben Dec 14 '11 at 01:34
  • What exactly do you mean by 'formatted'? You can program a stacktrace formatter yourself if you feel the need for it. But I'm not sure I understand what you want to achieve just yet. What would be a formatting that would make it easier for you? – mkro Dec 14 '11 at 20:53
  • I don't know... like I said, I'm new to java. I've seen a few posts around here about log viewers and thought there was a tool that I needed. – Ben Dec 14 '11 at 23:41