如何在 Java Web 服务中为 @WebParam 定义默认值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24441730/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 12:13:35  来源:igfitidea点击:

How to define a default value for a @WebParam in a java web service?

javaweb-servicessoapjaxbjax-ws

提问by Carlo Ya?ez

i have been trying to find a solution for this particular topic:

我一直在尝试为这个特定主题找到解决方案:

It is possible to define a default value in a @webParam annotation for a SOAP/WSDL based Web Service? What i want is exactly the following result:

是否可以在基于 SOAP/WSDL 的 Web 服务的 @webParam 注释中定义默认值?我想要的正是以下结果:

I Have a @WebMethod in my web service called getCustomers with the following signature:

我的 Web 服务中有一个名为 getCustomers 的 @WebMethod,其签名如下:

@WebMethod(operationName = "getCustomers")
@WebResult(name = "customer")
public Customer[] getCustomers(@WebParam(name = "company") String company,
       @WebParam(name = "limitTop") int limitTop){

       //Logic of getting customer be here

}

Particularly, the parameter limitTopis not optional in this method, this number indicates how many customer records you want the response returns, but i want that in my xml schema, this particular element has a default value, something like this:

特别是,参数limitTop在此方法中不是可选的,此数字表示您希望响应返回多少客户记录,但我希望在我的 xml 架构中,此特定元素具有默认值,如下所示:

<xs:complexType name="getCustomers">
    <xs:sequence>
        <xs:element name="company" type="xs:string" minOccurs="0"/>
        **<xs:element name="limitTop" type="xs:int" default="100"/>**
    </xs:sequence>
</xs:complexType>

Please notice the defaultattribute in the element named limitTop

请注意名为limitTop的元素中的默认属性

This is in order to allow to the client application send a SOAP message without the limitTopelement, but still limit the number of records returned to the default value given in the limitTop element.

这是为了允许客户端应用程序发送没有limitTop元素的 SOAP 消息,但仍将返回的记录数限制为 limitTop 元素中给定的默认值。

I am using Java, JAX-WS, and GlassFish Server Open Source Edition.

我正在使用 Java、JAX-WS 和 GlassFish Server 开源版。

Thanks for your help.

谢谢你的帮助。

Carlo Ya?ez

卡洛·亚兹

采纳答案by kolossus

What you're after cannot be implemented with your current webservice style. First, JAXB will not inject the default value into the message, ifthe element is not present in the original message sent. From the JAXB manual

您所追求的无法用您当前的网络服务风格实现。首先,如果发送的原始消息中不存在该元素,JAXB 不会将默认值注入到消息中。来自JAXB 手册

element defaults do not kick in when the element is absent, so unfortunately we can't change this behavior.

当元素不存在时,元素默认值不会生效,因此很遗憾我们无法更改此行为。

As a workaround, you could change your webservice style to WRAPPED. With this style, you can encapsulate your webservice parameters in a single parent type (a simple RequestWrapperif you will). Given that flexibility, your clients can supply a single parent type that may or may not contain both parameters. Your wrapper should look something like:

作为一种解决方法,您可以将您的网络服务样式更改为WRAPPED. 使用这种风格,您可以将您的 web 服务参数封装在一个单一的父类型中(RequestWrapper如果您愿意的话,这很简单)。鉴于这种灵活性,您的客户端可以提供可能包含也可能不包含这两个参数的单个父类型。您的包装器应如下所示:

@XmlRootElement
public class YourWrapper {

 String company;

 @XmlElement(name="limitTop",required="false",defaultValue="100")
 int limitTop; 

//getters and setters
}

Also,

还,

@RequestWrapper(targetNamespace="http://you.com/ws/types", className="com.you.YourWrapper") 
@WebMethod(operationName = "getCustomers")
@WebResult(name = "customer")
public Customer[] getCustomers(@WebParam(name = "company") String company,
       @WebParam(name = "limitTop") int limitTop){

       //Logic of getting customer be here

}

Given the above, JAXB will be able to ignore the absence of the value and possibly set the default afterthe unmarshalling

鉴于上述情况,JAXB 将能够忽略该值的缺失,并可能在解组设置默认值