JAXB는 XML 어댑터를 사용하여이 경우를 지원할 수 있습니다. 그들 사이의 변환
package blog.immutable.adpater;
import javax.xml.bind.annotation.XmlAttribute;
import blog.immutable.Address;
public class AdaptedCustomer {
private String name;
private Address address;
@XmlAttribute
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
}
와 XML 어댑터 : 당신은 단순히이 클래스의 매핑 가능한 버전을 만들 필요가
package blog.immutable;
public class Customer {
private final String name;
private final Address address;
public Customer(String name, Address address) {
this.name = name;
this.address = address;
}
public String getName() {
return name;
}
public Address getAddress() {
return address;
}
}
: 다음없이 제로 인수 생성자와 객체를 고려
을
: Customer 클래스를 참조 속성에 대한 다음
package blog.immutable.adpater;
import javax.xml.bind.annotation.adapters.XmlAdapter;
import blog.immutable.Customer;
public class CustomerAdapter extends XmlAdapter<AdaptedCustomer, Customer> {
@Override
public Customer unmarshal(AdaptedCustomer adaptedCustomer) throws Exception {
return new Customer(adaptedCustomer.getName(), adaptedCustomer.getAddress());
}
@Override
public AdaptedCustomer marshal(Customer customer) throws Exception {
AdaptedCustomer adaptedCustomer = new AdaptedCustomer();
adaptedCustomer.setName(customer.getName());
adaptedCustomer.setAddress(customer.getAddress());
return adaptedCustomer;
}
}
, 단순히 @XmlJavaTypeAdapter 주석을 사용 더 자세한 예를 들어
package blog.immutable;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import blog.immutable.adpater.CustomerAdapter;
@XmlRootElement(name="purchase-order")
public class PurchaseOrder {
private Customer customer;
@XmlJavaTypeAdapter(CustomerAdapter.class)
public Customer getCustomer() {
return customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
}
은 다음을 참조하십시오
직접 링크를 복사하거나 답변의 기본 사항을 설명하지 말고 자세한 설명이 필요하면 블로그 게시물에 연결하십시오. 이렇게하면 링크가 죽어 버린 경우에도 답을 얻을 수 있습니다. –
이것들은 내가 감사 할 필요가있는 것이다. –
나는 단지 링크를 복사하는 것에 동의한다. 이 답변은 내 통근 중에 휴대 전화에서 작업하도록 게시되었습니다. 나는 컴퓨터에있을 때 더 자세한 정보를 제공 할 것이다. 그게 도움이된다면 내 블로그에 연결됩니다. –