내 예에서는 JAXBContext.newInstance(T)
paramether Class가 필요하며 제네릭이있는이 솔루션은 작동하지 않습니다.
public class SerializationUtilJaxb<T> {
public String serialize(T jaxbObject) {
StringWriter stringWriter = new StringWriter();
try {
JAXBContext jaxbContext = JAXBContext.newInstance(T);
Marshaller objectMarshaller = jaxbContext.createMarshaller();
objectMarshaller.marshal(jaxbObject, stringWriter);
return stringWriter.toString();
} catch (JAXBException e) {
throw new RuntimeException(e);
}
}
}
이유를 물어봐도 될까요? 제네릭의 올바른 솔루션은 무엇입니까?
대신 무제한 와일드 카드 ?
를 사용하십시오 .
public static void SerializationUtilJaxb(Class<?> rootClass, Object rootObj) throws JAXBException, IOException{
try{
StringWriter stringWriter = new StringWriter();
JAXBContext context = JAXBContext.newInstance(rootClass);
Marshaller m = context.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
m.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
m.marshal(rootObj, stringwriter);
}
catch(Exception e){
// System.out.println (e.getMessage);
throw e;
}
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다