gpt4 book ai didi

java - 无法编码 java.lang.String

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:26:55 26 4
gpt4 key购买 nike

这是我的困境:

我有一个 dto 类,用于从 XML 来回编码。

诀窍是:由于我们的项目处理的 dto 类的数量是带有复数外部标记的集合,所以我决定创建一个委托(delegate)集合,它允许我使用其中一个类并毫不费力地将它们变成一个收集并获得随之而来的便利(迭代、添加等)。

在我们的项目中,我们有编码测试来清除注释错误等。下面是我的故障代码。

问题:根据编码器的不同,如果我扩展此 QuickCollection,我会收到以下错误。当使用 CXF 作为对 web 服务请求的响应将对象解码为 xml 时,它会失败。确切错误:com.sun.istack.SAXException2:无法将类型“java.lang.String”编码为元素,因为它缺少@XmlRootElement 注释

在测试中使用 JAXB 编码/取消编码时没问题。当使用相同的 QuickCollection 来编码来自使用 spring RestOperations 的第 3 方的结果并且工作正常时

脑洞大开:当我删除继承并将集合作为私有(private)成员管理时,一切正常!

这对我来说毫无意义,因为我在两种情况下都返回了确切的数据类型。

以下是所有相关代码。

这是继承的委托(delegate)类。

    public class QuickCollection<T> implements Collection<T> {
// to be set if needed after instantiation. To behave like a normal collection, we set it to something safe
protected Collection<T> delegate = Collections.emptySet();

public QuickCollection() {
}

public QuickCollection(Collection<T> delegate) {
this.delegate = delegate;
}

@Override
public int size() {
return delegate.size();
}

@Override
public boolean isEmpty() {
return delegate.isEmpty();
}

@Override
public boolean contains(Object o) {
return delegate.contains(o);
}

@Override
public Iterator<T> iterator() {
return delegate.iterator();
}

@Override
public Object[] toArray() {
return delegate.toArray();
}

@Override
public <T> T[] toArray(T[] a) {
return delegate.toArray(a);
}

@Override
public boolean add(T t) {
return delegate.add(t);
}

@Override
public boolean remove(Object o) {
return delegate.remove(o);
}

@Override
public boolean containsAll(Collection<?> c) {
return delegate.containsAll(c);
}

@Override
public boolean addAll(Collection<? extends T> c) {
return delegate.addAll(c);
}

@Override
public boolean removeAll(Collection<?> c) {
return delegate.removeAll(c);
}

@Override
public boolean retainAll(Collection<?> c) {
return delegate.retainAll(c);
}

@Override
public void clear() {
delegate.clear();
}

@Override
public String toString() {
return "" + delegate.toString();
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

QuickCollection that = (QuickCollection) o;

if (delegate != null ? !delegate.equals(that.delegate) : that.delegate != null) return false;

return true;
}

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

这是子DTO类

@XmlAccessorType(XmlAccessType.PROPERTY)
@XmlType(name = "BuddyCodes")
@XmlRootElement(name = "BuddyCodes")
public class BuddyCodes extends QuickCollection<String> implements Xml {

private Long accountId;

private Date expirationDate;

public BuddyCodes() {
super.delegate = new HashSet<String>();
}

public BuddyCodes(Long accountId, Set<String> codes, Date expirationDate) {
super(codes);
this.accountId = accountId;
this.expirationDate = expirationDate;
super.delegate = new HashSet<String>();

}

public BuddyCodes(Long accountId, Date expirationDate) {
this.accountId = accountId;
this.expirationDate = expirationDate;
super.delegate = new HashSet<String>();
}

@Override
public String toXml() {
String retVal;
try {
retVal = StringUtils.toXml(this);
}
catch (JAXBException e) {
retVal = e.toString();
}
return retVal;

}

public Long getAccountId() {
return accountId;
}

public void setAccountId(Long accountId) {
this.accountId = accountId;
}

public Set<String> getCodes() {
return (Set<String>) super.delegate;
}

@XmlElement(name = "code")
public void setCodes(Set<String> codes) {
super.delegate = codes;
}

public Date getExpirationDate() {
return expirationDate;
}

public void setExpirationDate(Date expirationDate) {
this.expirationDate = expirationDate;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

BuddyCodes that = (BuddyCodes) o;

if (accountId != null ? !accountId.equals(that.accountId) : that.accountId != null) return false;
if (delegate != null ? !super.delegate.equals(that.delegate) : that.delegate != null) return false;
if (expirationDate != null ? !expirationDate.equals(that.expirationDate) : that.expirationDate != null)
return false;

return true;
}

@Override
public int hashCode() {
int result = accountId != null ? accountId.hashCode() : 0;
result = 31 * result + (expirationDate != null ? expirationDate.hashCode() : 0);
result = 31 * result + (super.delegate != null ? super.delegate.hashCode() : 0);
return result;
}

@Override
public String toString() {
return "BuddyCodes{" +
"accountId=" + accountId +
"codes=" + super.delegate +
", expirationDate=" + expirationDate +
'}';
}
}

而且它不起作用。我收到错误。

现在,这是移除继承后的子类,它可以工作!!!

import javax.xml.bind.JAXBException;
import javax.xml.bind.annotation.*;
import java.util.Collection;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

/**
* @author christian.bongiorno
* Date: 10/3/11
* Time: 6:11 PM
*/
@XmlAccessorType(XmlAccessType.PROPERTY)
@XmlType(name = "BuddyCodes")
@XmlRootElement(name = "BuddyCodes")
public class BuddyCodes implements Xml {

private Long accountId;

private Date expirationDate;
private Set<String> delegate;
public BuddyCodes() {
delegate = new HashSet<String>();
}

public BuddyCodes(Long accountId, Set<String> codes, Date expirationDate) {
this.accountId = accountId;
this.expirationDate = expirationDate;
delegate = new HashSet<String>();

}

public BuddyCodes(Long accountId, Date expirationDate) {
this.accountId = accountId;
this.expirationDate = expirationDate;
delegate = new HashSet<String>();
}

@Override
public String toXml() {
String retVal;
try {
retVal = StringUtils.toXml(this);
}
catch (JAXBException e) {
retVal = e.toString();
}
return retVal;

}

public Long getAccountId() {
return accountId;
}

public void setAccountId(Long accountId) {
this.accountId = accountId;
}

public Set<String> getCodes() {
return delegate;
}

@XmlElement(name = "code")
public void setCodes(Set<String> codes) {
delegate = codes;
}

public Date getExpirationDate() {
return expirationDate;
}

public void setExpirationDate(Date expirationDate) {
this.expirationDate = expirationDate;
}

public boolean add(String s) {
return delegate.add(s);
}

public int size() {
return delegate.size();
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

BuddyCodes that = (BuddyCodes) o;

if (accountId != null ? !accountId.equals(that.accountId) : that.accountId != null) return false;
if (delegate != null ? !delegate.equals(that.delegate) : that.delegate != null) return false;
if (expirationDate != null ? !expirationDate.equals(that.expirationDate) : that.expirationDate != null)
return false;

return true;
}

@Override
public int hashCode() {
int result = accountId != null ? accountId.hashCode() : 0;
result = 31 * result + (expirationDate != null ? expirationDate.hashCode() : 0);
result = 31 * result + (delegate != null ? delegate.hashCode() : 0);
return result;
}


}

为什么继承很重要???

我还没有想出这个,但是,我有另一个类似布局的 DTO (BuddyTypes BuddyType)。 BuddyType 有 2 个成员:Long 和 String。两者都被注释为 XmlElement。这个工作得很好。

在我的问题案例中似乎没有注释组成委托(delegate)的集合成员的问题,我不知道如何注释父成员。作为一个继承类,拥有某种默认名称/注释是没有意义的。但是,我尝试了这种疯狂的做法,但注释被忽略了——我以前见过父成员注释被忽略,所以这不是什么新鲜事。

不知道可不可以,我需要注解一个父成员。

最佳答案

开箱即用:尝试 Simple XML库而不是 JAXB。我的经验是最好的。

关于java - 无法编码 java.lang.String,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7683713/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com