Java 隐藏 xml 消息中的实体变量 - @XmlTransient 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18603517/
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-12 09:24:41  来源:igfitidea点击:

Hide an entity variable from xml message - @XmlTransient not working

javaxmlweb-servicesjaxbxmltransient

提问by David031

I have an entity class:

我有一个实体类:

public class Customer implements Serializable {
private static final long serialVersionUID = 1L;

@XmlTransient
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "CUSTOMER_ID")
private Integer customerId;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 30)
@Column(name = "NAME")
private String name;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 30)
@Column(name = "ADDRESSLINE1")
private String addressline1;
@Basic(optional = false)
.
.
.
.

I sent an object of the class via xml in jax-ws web service like so:

我在 jax-ws Web 服务中通过 xml 发送了一个类的对象,如下所示:

<addressline1>xx</addressline1><addressline2>xx</addressline2><city>xx</city><country>xx</country><creditLimit>xx</creditLimit><customerId>xx</customerId><email>xx</email><name>xx</name><owner>xx</owner><phone>xx</phone><province>xx</province><zip>xx</zip>

Is it possible to not sent one of the variables such as customerId, which the client shouldn't see? I have added @XmlTransient, but no change.

是否可以不发送客户端不应该看到的变量之一,例如 customerId?我添加了@XmlTransient,但没有变化。

回答by bdoughan

By default public properties are serialized to XML. You will need to mark the corresponding getmethod @XmlTransient. If you wish to a annotate the fields you can add the following to your class @XmlAccessorType(XmlAccessType.FIELD).

默认情况下,公共属性被序列化为 XML。您将需要标记相应的get方法@XmlTransient。如果您希望对字段进行注释,您可以将以下内容添加到您的类中@XmlAccessorType(XmlAccessType.FIELD)

For More Information

想要查询更多的信息

回答by Carltham

Tried with @XmlTransientin different combinations with @XmlAccessorType(XmlAccessType.FIELD), on my machine it did not work.

尝试了与@XmlTransient的不同组合@XmlAccessorType(XmlAccessType.FIELD),在我的机器上它不起作用。

What did work for me, to hide functions, was to annotate each function with @WebMethod(exclude = true), to hide it from the wsdland thereby exposure to the client.

对我有用的是隐藏函数,是用 注释每个函数@WebMethod(exclude = true),将它隐藏起来wsdl,从而暴露给客户端。