【问题标题】:How to Mention XS:MaxLength in xml simpleType from Java XML Binding using annotations如何在 Java XML Binding 中使用注释在 xml simpleType 中提及 XS:MaxLength
【发布时间】:2015-08-27 12:29:24
【问题描述】:

我想在下面的 XML 代码中显示 maxLength 属性。 我需要在我的 java 类中做什么。

JAVA Class need to modified occording the below xml:

public class xxxx{
 protected String directionsToSite;
}

===========================================================
XML Expected will be like this:

  <element name="accessToAntennas" minOccurs="0">
            <simpleType>
             <restriction base="{http://www.w3.org/2001/XMLSchema}string">
               <maxLength value="500"/>
            <restriction>
           <simpleType>
         </element>

【问题讨论】:

    标签: java xml web-services jakarta-ee xml-binding


    【解决方案1】:

    尝试使用注解@Size(min=..., max=...) 或者你可以试试:

    public class TheClazz {
      @XmlJavaTypeAdapter(value = MyXmlAdapter.class, type = String.class)
      private String myString;
    }
    
    public class MyXmlAdapter extends XmlAdapter<String, String> {
    
        private final int MAX = 500; 
    
        @Override
        public String unmarshal(String s) throws Exception {
            return s.size() > MAX ? throw new Exception() : s; 
        }
    
        @Override
        public String marshal(String s) throws Exception {
            return s.substring(0, MAX);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-21
      • 2013-06-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-08
      相关资源
      最近更新 更多