java wsdl 参数必填设置

原来的wsdl

<xs:complexType name="SyncPartTraceProcess">
<xs:sequence>
<xs:element minOccurs="0" name="OrderMasterData" type="tns:partTraceWrap"/>
xs:sequence>
xs:complexType>
<xs:complexType name="partTraceWrap">
<xs:complexContent>
<xs:extension base="tns:baseWrap">
<xs:sequence>
<xs:element minOccurs="0" name="data" type="xs:string"/>
xs:sequence>
xs:extension>
xs:complexContent>
xs:complexType>
<xs:complexType name="baseWrap">
<xs:sequence>
<xs:element minOccurs="0" name="key" type="xs:string"/>
xs:sequence>
xs:complexType>

修改要求

下面是调用方给的截图,参数是0…1,表示可空,业务方要求使用1…1
java wsdl 参数必填设置_第1张图片

分析

从wsdl看来,关键是minOccurs="0"表示0…1,只要把它去掉,或者修改成 `minOccurs="1"即可, 那怎么去修改呢?

1:首先根结点

<xs:element minOccurs="0" name="OrderMasterData" type="tns:partTraceWrap"/>

OrderMasterData这个是根参数里面的。
2:其次是根结点下的元素。

<xs:element minOccurs="0" name="data" type="xs:string"/>

调整

1:根结代码调整。

import com.ly.mp.swcas.main.wrap.*;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.xml.bind.annotation.XmlElement;
import java.text.ParseException;

@WebService(name = "SwcasService")
public interface SwcasService {
    @WebMethod
    @WebResult(name = "WsResult", targetNamespace = "")

    WsResult SyncPartTraceProcess(@WebParam(name = "OrderMasterData") @XmlElement(required = true) PartTraceWrap data) throws ParseException;

}

PS:在方法的参数上添加@XmlElement(required = true),表示这个参数是必填的。

2:叶结代码调整。叶结点实际上是上面PartTraceWrap 参数实体的一个属性。调整如下

package com.ly.mp.swcas.main.wrap;

import javax.xml.bind.annotation.*;
import java.io.Serializable;


@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class PartTraceWrap extends BaseWrap implements Serializable {
    private static final long serialVersionUID = -6763186241763563647L;
    @XmlElement(required = true)
    private  String data="";
    public String getData() {
        return data;
    }
    public void setData(String data) {
        this.data = data;
    }
}

PS:在data字段添加上@XmlElement(required = true)注解,在类PartTraceWrap上添加 @XmlRootElement注解。
  注意,两个注释都必在有,只添加字段注释,项目是启动不了的

3:修改完成,看一下重新生成的wsdl效果,已然生效。

<xs:complexType name="SyncPartTraceProcess">
<xs:sequence>
<xs:element name="OrderMasterData" type="tns:partTraceWrap"/>
xs:sequence>
xs:complexType>
<xs:complexType name="partTraceWrap">
<xs:complexContent>
<xs:extension base="tns:baseWrap">
<xs:sequence>
<xs:element name="data" type="xs:string"/>
xs:sequence>
xs:extension>
xs:complexContent>
xs:complexType>
<xs:complexType name="baseWrap">
<xs:sequence>
<xs:element minOccurs="0" name="key" type="xs:string"/>
xs:sequence>
xs:complexType>

总结

关键是 @XmlElement(required = true) 注解,把原代码关键部分抽出来。如下


@Retention(RUNTIME) @Target({FIELD, METHOD, PARAMETER})
public @interface XmlElement {
    String name() default "##default";
    boolean nillable() default false;

    /**
     * Customize the element declaration to be required.
     * 

If required() is true, then Javabean property is mapped to * an XML schema element declaration with minOccurs="1". * maxOccurs is "1" for a single valued property and "unbounded" * for a multivalued property. *

If required() is false, then the Javabean property is mapped * to XML Schema element declaration with minOccurs="0". * maxOccurs is "1" for a single valued property and "unbounded" * for a multivalued property. */ boolean required() default false; String namespace() default "##default"; String defaultValue() default "\u0000"; Class type() default DEFAULT.class; static final class DEFAULT {} }

1:从代码的注释可以看到,required 值对应 minOccurs值为: false : 0 ;true : 1。
2:本注解 使用了 @Retention(RUNTIME) @Target({FIELD, METHOD, PARAMETER}) 注解,表示可以用于字段,方法,参数。

你可能感兴趣的:(Java)