Java 日蚀线MOXy,对象图中的循环
是否有方法封送/取消封送以下用户实体对象? 一些特别的注释 抽象类中的用户是编辑用户实体对象的用户。 这两个用户完全相同是完全合法的。 执行以下代码将引发以下异常:Java 日蚀线MOXy,对象图中的循环,java,eclipselink,moxy,Java,Eclipselink,Moxy,是否有方法封送/取消封送以下用户实体对象? 一些特别的注释 抽象类中的用户是编辑用户实体对象的用户。 这两个用户完全相同是完全合法的。 执行以下代码将引发以下异常: org.eclipse.persistence.exceptions.xmlmarshallexception 异常描述:在对象图中检测到循环 import java.io.StringReader; import java.io.StringWriter; import javax.xml.bind.JAXBContext; im
org.eclipse.persistence.exceptions.xmlmarshallexception
异常描述:在对象图中检测到循环
import java.io.StringReader;
import java.io.StringWriter;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlRootElement;
import org.eclipse.persistence.jaxb.JAXBContextFactory;
import org.eclipse.persistence.jaxb.MarshallerProperties;
import org.eclipse.persistence.oxm.MediaType;
public class UserApp {
public static abstract class AbstractEntity {
private Long id;
private User user;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public void setId(Long id) {
this.id = id;
}
public Long getId() {
return id;
}
}
@XmlRootElement
public static class User extends AbstractEntity {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public static void main(String[] args) throws JAXBException {
UserApp userApp = new UserApp();
User user = new User();
user.setId(1L);
user.setName("Jim");
user.setUser(user);
String marshalledUser = userApp.marshal(user);
System.out.println(marshalledUser);
User unmarshalledUser = userApp.unmarshal(User.class, new StringReader(marshalledUser));
}
private String marshal(Object toMarshal) throws JAXBException {
JAXBContext jc = JAXBContextFactory.createContext(new Class[] {toMarshal.getClass()}, null);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.setProperty(MarshallerProperties.MEDIA_TYPE, MediaType.APPLICATION_JSON);
StringWriter sw = new StringWriter();
marshaller.marshal(toMarshal, sw);
return sw.toString();
}
private <T> T unmarshal(Class<T> entityClass, StringReader sr) throws JAXBException {
JAXBContext jc = JAXBContextFactory.createContext(new Class[] {entityClass}, null);
Unmarshaller unmarshaller = jc.createUnmarshaller();
unmarshaller.setProperty(MarshallerProperties.MEDIA_TYPE, MediaType.APPLICATION_JSON);
return (T) unmarshaller.unmarshal(sr);
}
}
导入java.io.StringReader;
导入java.io.StringWriter;
导入javax.xml.bind.JAXBContext;
导入javax.xml.bind.JAXBException;
导入javax.xml.bind.Marshaller;
导入javax.xml.bind.Unmarshaller;
导入javax.xml.bind.annotation.XmlRootElement;
导入org.eclipse.persistence.jaxb.JAXBContextFactory;
导入org.eclipse.persistence.jaxb.MarshallerProperties;
导入org.eclipse.persistence.oxm.MediaType;
公共类用户应用{
公共静态抽象类AbstractEntity{
私人长id;
私人用户;
公共用户getUser(){
返回用户;
}
公共void setUser(用户){
this.user=用户;
}
公共无效集合id(长id){
this.id=id;
}
公共长getId(){
返回id;
}
}
@XmlRootElement
公共静态类用户扩展抽象实体{
私有字符串名称;
公共字符串getName(){
返回名称;
}
公共void集合名(字符串名){
this.name=名称;
}
}
公共静态void main(字符串[]args)抛出JAXBEException{
UserApp UserApp=新的UserApp();
用户=新用户();
用户设置ID(1L);
user.setName(“Jim”);
user.setUser(用户);
字符串marshalledUser=userApp.marshall(用户);
System.out.println(marshalledUser);
User unmarshalledUser=userApp.unmarshal(User.class,新StringReader(marshalledUser));
}
私有字符串封送处理(对象toMarshal)抛出JAXBEException{
JAXBContext jc=JAXBContextFactory.createContext(新类[]{toMarshal.getClass()},null);
Marshaller=jc.createMarshaller();
setProperty(marshaller.JAXB_格式化的_输出,true);
setProperty(MarshallerProperties.MEDIA\u TYPE、MediaType.APPLICATION\u JSON);
StringWriter sw=新的StringWriter();
元帅,元帅(西南部托马沙尔);
返回sw.toString();
}
私有T unmarshal(类entityClass,StringReader sr)抛出JAXBEException{
JAXBContext jc=JAXBContextFactory.createContext(新类[]{entityClass},null);
Unmarshaller Unmarshaller=jc.createUnmarshaller();
unmarshaller.setProperty(MarshallerProperties.MEDIA\u TYPE、MediaType.APPLICATION\u JSON);
返回(T)解组器。解组器(sr);
}
}
当前,MOXy不支持映射双向关系,其中两个方向使用相同的属性。您可以使用以下链接跟踪我们在该问题上的进展: