使用非数据库字段自定义Liferay Web Service响应

时间:2016-12-15 16:48:45

标签: java java-ee liferay liferay-7 liferay-service-builder

在Liferay 7中,我使用带有getter / setter的新字段自定义了我的FooImpl.java(由Foo表中的服务构建器生成):

@ProviderType
public class FooImpl extends FooBaseImpl {

    private String toto;
    // and getter and setter

    public FooImpl() {
    }

}

我添加此字段是因为我希望它在以下方法的Web Service响应中(从FooServiceImpl.java中提取):

@JSONWebService(value = "get-foos", method = "GET")
@AccessControlled(guestAccessEnabled=true)
public List<Foo> getFoos(){
   ...
}

不幸的是,JSON响应并未包含自定义字段&#34; toto&#34;。

有人知道怎么做吗?

2 个答案:

答案 0 :(得分:2)

非常感谢Daniele。 我在您提供的文档中找到了答案。

实际上,它非常简单,只需将注释@JSON(strict = false)添加到模型对象中,所有自定义属性都将被序列化。

@JSON(strict = false)
@ProviderType
public class FooImpl extends FooBaseImpl {

    private String toto;
    // and getter and setter

    public FooImpl() {
    }

}

答案 1 :(得分:1)

看看这个维基页面。我没有这个主题的直接经验。如果这是正确的方法,请告诉我。

https://web.liferay.com/it/community/wiki/-/wiki/Main/JSON+Serialization#section-JSON+Serialization-Strict+mode