Java 如何封送/解封送地图<;整数,列表<;整数>>;?

Java 如何封送/解封送地图<;整数,列表<;整数>>;?,java,jaxb,moxy,Java,Jaxb,Moxy,以下程序封送和解封送包含Map字段的类 解组后,映射中的列表包含字符串,而不是整数 有没有一种简单的方法来确保列表将由整数而不是整数填充 解组期间的字符串? import java.io.StringReader; import java.io.StringWriter; import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.xml.

以下程序封送和解封送包含
Map
字段的类

解组后,映射中的列表包含字符串,而不是整数

有没有一种简单的方法来确保列表将由整数而不是整数填充 解组期间的字符串?

import java.io.StringReader;
import java.io.StringWriter;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
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 MapApp {

    @XmlRootElement
    public static class Publication {

        private String name;

        private Map<Integer, List<Integer>> yearToIssues;

        public void setName(String name) {
            this.name = name;
        }

        public String getName() {
            return name;
        }

        public void setYearToIssues(Map<Integer, List<Integer>> yearToIssues) {
            this.yearToIssues = yearToIssues;
        }

        public Map<Integer, List<Integer>> getYearToIssues() {
            return yearToIssues;
        }

    }

    public static void main(String[] args) throws JAXBException {
        Publication publication = new Publication();
        publication.setName("JAXB miracles");
        Map<Integer, List<Integer>> yearToIssues = new HashMap<>();
        yearToIssues.put(2013, Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12));
        yearToIssues.put(2014, Arrays.asList(1, 2));
        publication.setYearToIssues(yearToIssues);
        String marshalled = marshal(publication);
        Publication uPublication = unmarshal(Publication.class, marshalled);
        List<Integer> issues = uPublication.getYearToIssues().get(2013);
        if (((Object) issues.get(0)) instanceof String) {
            System.out.println("issue is instance of String!");
        }

    }

    static 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_XML);
        StringWriter sw = new StringWriter();
        marshaller.marshal(toMarshal, sw);
        System.out.println(sw);
        return sw.toString();
    }

    static <T> T unmarshal(Class<T> entityClass, String str) throws JAXBException {
        JAXBContext jc = JAXBContextFactory.createContext(new Class[] {entityClass}, null);
        Unmarshaller unmarshaller = jc.createUnmarshaller();
        unmarshaller.setProperty(MarshallerProperties.MEDIA_TYPE, MediaType.APPLICATION_XML);
        return (T) unmarshaller.unmarshal(new StringReader(str));
    }

}
导入java.io.StringReader;
导入java.io.StringWriter;
导入java.util.array;
导入java.util.HashMap;
导入java.util.List;
导入java.util.Map;
导入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;
公共类MapApp{
@XmlRootElement
公共静态类发布{
私有字符串名称;
私人地图年鉴;
公共void集合名(字符串名){
this.name=名称;
}
公共字符串getName(){
返回名称;
}
公共无效设置yearToIssues(映射yearToIssues){
this.yearToIssues=yearToIssues;
}
公共地图getYearToIssues(){
返回问题的年份;
}
}
公共静态void main(字符串[]args)抛出JAXBEException{
Publication Publication=新发布();
publication.setName(“JAXB奇迹”);
Map yearToIssues=new HashMap();
yearToIssues.put(2013,Arrays.asList(1,2,3,4,5,6,7,8,9,10,11,12));
年发行量(2014,数组。asList(1,2));
出版物。设置年刊(年刊);
字符串封送=封送(发布);
Publication uppublication=unmarshal(Publication.class,封送);
列表问题=uppublication.getYearToIssues().get(2013);
if(((对象)issues.get(0))instanceof String){
System.out.println(“问题是字符串的实例!”);
}
}
静态字符串封送处理(对象toMarshal)抛出JAXBEException{
JAXBContext jc=JAXBContextFactory.createContext(新类[]{toMarshal.getClass()},null);
Marshaller=jc.createMarshaller();
setProperty(marshaller.JAXB_格式化的_输出,true);
setProperty(MarshallerProperties.MEDIA\u TYPE、MediaType.APPLICATION\u XML);
StringWriter sw=新的StringWriter();
元帅,元帅(西南部托马沙尔);
系统输出打印LN(软件);
返回sw.toString();
}
静态T unmarshal(类entityClass,字符串str)抛出JAXBEException{
JAXBContext jc=JAXBContextFactory.createContext(新类[]{entityClass},null);
Unmarshaller Unmarshaller=jc.createUnmarshaller();
unmarshaller.setProperty(MarshallerProperties.MEDIA\u TYPE、MediaType.APPLICATION\u XML);
return(T)unmarshaller.unmarshal(newstringreader(str));
}
}

您需要为
Map
编写
XmlAdapter
,以正确处理此用例


Thx,因此我实现了一个XmlAdapter,将映射编组为字符串,反之亦然。格式如下:2013:1,2,3,4,5,6,7,8,9,10,11,12;2014:1,2