0

私はJavaでプログラムを開発しており、Jacksonを使用して出力XMLとJSONを使用しています。現時点では、JSONではなくXMLで出力すると、エラーが発生します。

エラーは次のエラーメッセージです

私が出力しようとしているクラスは、このクラスインスタンスがデータとして含まれているクラスです。JSONをリクエストすると、この出力が表示されます。

Error Message
    Jan 27, 2013 12:02:55 AM com.sun.jersey.spi.container.ContainerResponse logException
SEVERE: Mapped exception to response: 500 (Internal Server Error)
javax.ws.rs.WebApplicationException: javax.xml.bind.MarshalException
 - with linked exception:
[com.sun.istack.internal.SAXException2: class pt.eoliveira.appmanager.dao.Users nor any of its super class is known to this context.
    javax.xml.bind.JAXBException: class pt.eoliveira.appmanager.dao.Users nor any of its super class is known to this context.]
    at com.sun.jersey.core.provider.jaxb.AbstractRootElementProvider.writeTo(AbstractRootElementProvider.java:159)
    at com.sun.jersey.spi.container.ContainerResponse.write(ContainerResponse.java:306)
    at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1448)
    at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1360)
    at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1350)
    at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:416)
    at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:538)
    at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:716)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1002)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:585)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
    at java.lang.Thread.run(Thread.java:722)
Caused by: javax.xml.bind.MarshalException
 - with linked exception:
[com.sun.istack.internal.SAXException2: class pt.eoliveira.appmanager.dao.Users nor any of its super class is known to this context.
javax.xml.bind.JAXBException: class pt.eoliveira.appmanager.dao.Users nor any of its super class is known to this context.]
    at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:311)
    at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.marshal(MarshallerImpl.java:236)
    at javax.xml.bind.helpers.AbstractMarshallerImpl.marshal(AbstractMarshallerImpl.java:95)
    at com.sun.jersey.core.provider.jaxb.AbstractRootElementProvider.writeTo(AbstractRootElementProvider.java:179)
    at com.sun.jersey.core.provider.jaxb.AbstractRootElementProvider.writeTo(AbstractRootElementProvider.java:157)
    ... 27 more
Caused by: com.sun.istack.internal.SAXException2: class pt.eoliveira.appmanager.dao.Users nor any of its super class is known to this context.
javax.xml.bind.JAXBException: class pt.eoliveira.appmanager.dao.Users nor any of its super class is known to this context.
    at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.reportError(XMLSerializer.java:235)
    at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.reportError(XMLSerializer.java:250)
    at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.childAsXsiType(XMLSerializer.java:642)
    at com.sun.xml.internal.bind.v2.runtime.property.SingleElementNodeProperty.serializeBody(SingleElementNodeProperty.java:143)
    at com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.serializeBody(ClassBeanInfoImpl.java:343)
    at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.childAsSoleContent(XMLSerializer.java:582)
    at com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.serializeRoot(ClassBeanInfoImpl.java:325)
    at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.childAsRoot(XMLSerializer.java:483)
    at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:308)
    ... 31 more
Caused by: javax.xml.bind.JAXBException: class pt.eoliveira.appmanager.dao.Users nor any of its super class is known to this context.
    at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getBeanInfo(JAXBContextImpl.java:573)
    at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.childAsXsiType(XMLSerializer.java:637)
    ... 37 more

メッセージラッパー

package pt.eoliveira.appmanager.wrappers;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "message")
public class Message {

    private int error = 0;
    private String errorMessage = "";
    private Object data;

    public Message() {
    }

    public Message(int error, String errorMessage) {
        this.error = error;
        this.errorMessage = errorMessage;
    }

    public Message(Object data) {
        this.data = data;
    }

    public Message setData(Object data) {
        this.data = data;
        return this;
    }

    public Message setError(int error, String errorMessage) {
        this.error = error;
        this.errorMessage = errorMessage;
        return this;
    }

    public int getError() {
        return error;
    }

    public String getErroMessage() {
        return errorMessage;
    }

    public Object getData() {
        return data;
    }
}

データオブジェクト内に入るクラス

package pt.eoliveira.appmanager.dao;

import java.io.Serializable;
import java.security.NoSuchAlgorithmException;
import java.util.Collection;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;
import org.codehaus.jackson.annotate.JsonIgnore;
import pt.eoliveira.appmanager.utils.MySqlTools;

@Entity
@Table(name = "users")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Users.findAll", query = "SELECT u FROM Users u"),
    @NamedQuery(name = "Users.findByUserId", query = "SELECT u FROM Users u WHERE u.userId = :userId"),
    @NamedQuery(name = "Users.findByUsername", query = "SELECT u FROM Users u WHERE u.username = :username"),
    @NamedQuery(name = "Users.findByPassword", query = "SELECT u FROM Users u WHERE u.password = :password"),
    @NamedQuery(name = "Users.findByEmail", query = "SELECT u FROM Users u WHERE u.email = :email"),
    @NamedQuery(name = "Users.findByPermissions", query = "SELECT u FROM Users u WHERE u.permissions = :permissions")
})

public class Users implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "user_id")
    private Integer userId;

    @Basic(optional = false)
    @Column(name = "username")
    private String username;

    @Basic(optional = false)
    @Column(name = "password")
    @XmlTransient
    @JsonIgnore
    private String password;

    @Basic(optional = false)
    @Column(name = "email")
    private String email;

    @Basic(optional = false)
    @Column(name = "permissions")
    private int permissions;

    @OneToOne(mappedBy = "owner")
    private Sessions sessions;

    @JoinColumn(name = "country", referencedColumnName = "country_id")
    @ManyToOne(optional = false)
    private Country country;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "owner")
    private Collection<Apps> appsCollection;

    public Users() {
    }

    public Users(String username) {
        this.username = username;
    }

    public Users(Integer userId) {
        this.userId = userId;
    }

    public Users(Integer userId, String username, String password, String email, int permissions) {
        this.userId = userId;
        this.username = username;
        this.password = password;
        this.email = email;
        this.permissions = permissions;
    }

    public Integer getUserId() {
        return userId;
    }

    public void setUserId(Integer userId) {
        this.userId = userId;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public int getPermissions() {
        return permissions;
    }

    public void setPermissions(int permissions) {
        this.permissions = permissions;
    }

    public Sessions getSessions() {
        return sessions;
    }

    public void setSessions(Sessions sessions) {
        this.sessions = sessions;
    }

    public Country getCountry() {
        return country;
    }

    public void setCountry(Country country) {
        this.country = country;
    }

    @XmlTransient
    public Collection<Apps> getAppsCollection() {
        return appsCollection;
    }

    public void setAppsCollection(Collection<Apps> appsCollection) {
        this.appsCollection = appsCollection;
    }

    public boolean isValid(String password) {
        try {
            return this.password.equals(MySqlTools.MD5Encode(password));
        } catch (NoSuchAlgorithmException ex) {
            ex.printStackTrace();
        }
        return false;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (userId != null ? userId.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 Users)) {
            return false;
        }
        Users other = (Users) object;
        if ((this.userId == null && other.userId != null) || (this.userId != null && !this.userId.equals(other.userId))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "Users{" + "userId=" + userId + ", username=" + username + ", password=" + password + ", email=" + email + ", permissions=" + permissions + ", sessions=" + sessions + ", country=" + country + ", appsCollection=" + appsCollection + '}';
    }
}

Json出力の例

    {"error":0,"data":
    {"userId":17,"username":"xxxx","password":"xxxx","email":"xxxx","permissions":1,"sessions":
    {"sessionId":8,"sessionKey":"b0880029fae3b4588c37640f8452b2da","creationStamp":2017052136},"country":{"countryId":177,"iso2":"PT","shortName":"Portugal","longName":"Portuguese Republic","iso3":"PRT","numcode":"620","unMember":"yes","callingCode":"351","cctld":".pt"}},
    "erroMessage":""}

出力を生成する方法は次のとおりです。

@GET
@Path("validate")
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public Message validate(@QueryParam("username") String username, @QueryParam("password") String password) throws NoSuchAlgorithmException {
        Users rtn = validateUser(username, password);
        if (rtn != null) {
                if (rtn.getSessions() == null) {
                        rtn.setSessions(new Sessions(MySqlTools.MD5Encode(System.currentTimeMillis() + MySqlTools.SALT), (int) System.currentTimeMillis(), rtn));
                        super.edit(rtn);
                }
                return new Message(rtn);
        }
        return new Message(1,"Invalid Username or Password");
}

私にとって奇妙な部分は、出力をXMLとして取得しようとしたときにのみエラーが発生するのはなぜですか?...

4

0 に答える 0