8

我使用 JAXB 进行解组过程,请求来自 UI 到我们的服务类。下面是 XML 请求的格式。

<SampleRequest  user="testUser"  account="testAccount"    Specifier=  "value1a,value1b,value1c : name2a,value2b,value2c"/>

我的要求是,Specifier 属性有多个值系列(:冒号分隔)我需要将每个系列值映射到我的自定义 java 类

我试过这种方式

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class SampleRequest {

    @XmlAttribute
    private String user;

    @XmlAttribute
    private String account;


    @XmlAttribute(name="Specifier")
    private List<Specifier> specifier;


}

说明符.java

@XmlJavaTypeAdapter(SpecifierAdapter.class)
public class Specifier {



}

说明符适配器.java

public class SpecifierAdapter  extends XmlAdapter{

    @Override
    public Object marshal(Object arg0) throws Exception {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Object unmarshal(Object arg0) throws Exception {
        // TODO Auto-generated method stub
        return null;
    }

}

编辑部分

类说明符有 3 个字符串属性。

class Specifier
{
String value1;
String value2;
String value3;
}

我需要每个系列的说明符例如 (value1a,value1b,value1c) 应该分别映射到 value1 , value2 , value3

编辑第 3 部分

嗨, 感谢您的回复,我试图解组这个例子,我发现,我得到了空

这是我通过的请求

<sampleRequest user="user" account="account" Specifier="v1,v2,v3 : a1,a2,a3"/>

只是想确保,我的说明符类是否正确?(因为我在这里没有使用任何注释)

package com;

import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

@XmlJavaTypeAdapter(SpecifierAdapter.class)
public class Specifier {


    Specifier(String v1 , String v2 , String v3)
    {

    }

    String value1;

    public String getValue1() {
        return value1;
    }

    public void setValue1(String value1) {
        this.value1 = value1;
    }

    public String getValue2() {
        return value2;
    }

    public void setValue2(String value2) {
        this.value2 = value2;
    }

    public String getValue3() {
        return value3;
    }

    public void setValue3(String value3) {
        this.value3 = value3;
    }

    String value2;
    String value3;

}
4

3 回答 3

7

注意:适配器代码可以通过使用 guava-library 的Joiner 和 Splitter来简化。

样品申请

public class SampleRequest
{
    @XmlAttribute
    private String user;

    @XmlAttribute
    private String account;

    @XmlAttribute(name = "Specifier")
    @XmlJavaTypeAdapter(SpecifierAdapter.class)
    private List<Specifier> specifier;
}

适配器

public class SpecifierAdapter extends XmlAdapter<String, List<Specifier>>
{
    @Override
    public List<Specifier> unmarshal(final String v) throws Exception
    {
        String[] values = v.split(":");
        List<Specifier> l = new ArrayList<Specifier>();
        for (String s : values)
        {
            String[] vs = s.split(",");
            l.add(new Specifier(vs[0], vs[1], vs[2]));
        }
        return l;
    }

    @Override
    public String marshal(final List<Specifier> v) throws Exception
    {
        String values = "";
        for (Specifier s : v)
        {
            values += s.getValue1() + "," + s.getValue2() + "," + s.getValue3() + " : ";
        }
        return values.length() > 0 ? values.substring(0, values.length() - 3) : values;
    }
}

用法

public static void main(final String a[]) throws JAXBException
{
        SampleRequest r = new SampleRequest();
        r.setAccount("account");
        r.setUser("user");
        List<Specifier> sps = new ArrayList<Specifier>();
        sps.add(new Specifier("v1", "v2", "v3"));
        sps.add(new Specifier("a1", "a2", "a3"));
        r.setSpecifier(sps);

        JAXBContext jc = JAXBContext.newInstance(SampleRequest.class);
        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(r, System.out);
}

输出

<?xml version="1.0" encoding="UTF-8"?>
<sampleRequest user="user" account="account" Specifier="v1,v2,v3 : a1,a2,a3"/>
于 2012-08-10T14:23:07.760 回答
2

我已经用我的 IDE 准备好了答案。

您删除了您的问题(11887278)并再次询问。

我想了想,认为这个要求不是一个好主意。我只是对你的问题感兴趣,因为我从来没有对一个属性做这些多个字符串。

有趣的是,我的代码与 Senthil Kumar 解释的非常相似。

请在https://code.google.com/p/jinahya/source/browse/trunk/com.googlecode.jinahya/stackoverflow/src/main/java/com/googlecode/jinahya/stackoverflow/q11887278查看完整的 mavenized 项目/

package com.googlecode.jinahya.stackoverflow.q11887278;


import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.xml.bind.annotation.adapters.XmlAdapter;


/**
 *
 * @author Jin Kwon <jinahya at gmail.com>
 */
public class SpecifierAdpater extends XmlAdapter<String, List<Specifier>> {


    @Override
    public List<Specifier> unmarshal(final String value) throws Exception {

        if (value == null) {
            return null;
        }

        final List<Specifier> bound = new ArrayList<Specifier>();
        for (String split : value.split(":")) {
            final Specifier specifier = new Specifier();
            specifier.fromString(split);
            bound.add(specifier);
        }
        return bound;
    }


    @Override
    public String marshal(final List<Specifier> bound) throws Exception {

        if (bound == null) {
            return null;
        }

        final StringBuilder builder = new StringBuilder();
        final Iterator<Specifier> specifiers = bound.iterator();
        if (specifiers.hasNext()) {
            builder.append(specifiers.next().toString());
        }
        while (specifiers.hasNext()) {
            builder.append(":");
            builder.append(specifiers.next().toString());
        }
        return builder.toString();
    }


}

这里有 10 个示例,每个示例都是编组的和未编组的

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<request user="user"/>

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<request/>

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<request user="user" account="account"/>

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<request specifiers="null,null,value3:null,value2,value3"/>

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<request specifiers="value1,null,value3"/>

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<request/>

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<request user="user" account="account" specifiers="value1,null,null"/>

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<request user="user" account="account" specifiers="null,null,null"/>

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<request specifiers="null,value2,value3"/>

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<request user="user" account="account"/>
于 2012-08-10T14:39:18.933 回答
1

您可以为 SpecifierAdapter.java 尝试以下实现

public class SpecifierAdapter extends XmlAdapter<String, List<Specifier>>{
  @Override
  public String marshal(List<Specifier> values) throws Exception {
    StringBuilder result = new StringBuilder();
    for(Specifier specifier : values) {
      result.append(specifier.toString()); //assuming you have overridden toString() method
      result.append(",");
    }
    return result.length>0? result.substring(0, result.length() - 1): "";
  }

  @Override
  public List<Specifier> unmarshal(String colonSeparated) throws Exception {   
    List<String> values =  Arrays.asList(colonSeparated.split(":"));
    List<Specifier> specifiers = new ArrayList<Specifiers>();
    for(String str : values){
        specifiers.add(new Specifier(str)); //here use your constructor appropriately
    }
    return specifiers;
  }
}
于 2012-08-10T13:51:41.443 回答