Java 从struts rest插件请求时如何排除属性?
我正在使用struts rest插件Java 从struts rest插件请求时如何排除属性?,java,xml,json,rest,struts2,Java,Xml,Json,Rest,Struts2,我正在使用struts rest插件index方法,以xml或json格式返回我的Fruit对象列表。它工作得非常好 模型类: class Fruit { private String name; private double price; // constructor // getter/setter // equals and hash method } 我想从xml/json输出中的模型对象中排除一些属性,比如price。我知道我可以用一
index
方法,以xml
或json
格式返回我的Fruit
对象列表。它工作得非常好
模型类:
class Fruit {
private String name;
private double price;
// constructor
// getter/setter
// equals and hash method
}
我想从xml/json输出中的模型对象中排除一些属性,比如price。我知道我可以用一个包装器类来包装它,但它似乎有很多事情要做
我所尝试的:
@Results(@Result(name = "success", type = "redirectAction", params = {
"actionName", "fruit"}))
public class FruitController extends ActionSupport implements
ModelDriven<Object> {
private int id;
private Fruit model = new Fruit();
private List<Fruit> list;
private FruitService fruitService = new FruitService();
public void setId(int id) {
this.id = id;
if (id > 0) {
this.model = fruitService.get(this.id);
}
}
public int getId() {
return this.id;
}
public HttpHeaders index() {
list = fruitService.getAll();
return new DefaultHttpHeaders("index").disableCaching();
}
@Override
public Object getModel() {
return (list != null ? list : model);
}
....
}
@Results(@Result(name=“success”,type=“redirectAction”,params={
“actionName”、“水果”})
公共类控制器扩展了ActionSupport实现
模型驱动{
私有int-id;
私有水果模型=新水果();
私人名单;
private FruitService FruitService=新的FruitService();
公共无效集合id(内部id){
this.id=id;
如果(id>0){
this.model=fruitService.get(this.id);
}
}
公共int getId(){
返回此.id;
}
公共HttpHeaders索引(){
list=fruitService.getAll();
返回新的DefaultHttpHeaders(“索引”).disableCaching();
}
@凌驾
公共对象getModel(){
返回(列表!=null?列表:模型);
}
....
}
struts.xml
...
<constant name="struts.convention.action.suffix" value="Controller"/>
<constant name="struts.convention.action.mapAllMatches" value="true"/>
<constant name="struts.convention.default.parent.package" value="rest-default"/>
<constant name="struts.convention.package.locators" value="controller" />
...
<interceptor-ref name="params">
<param name="excludeParams">price</param>
</interceptor-ref>
...
。。。
...
价格
...
它不起作用了。请帮忙。谢谢。您应该使用XStream annotation从结果中省略所需字段我不在
拦截器上设置排除参数
。我在result
上使用excludeProperties
,您可以参考http://struts.apache.org/release/2.2.x/docs/json-plugin.html
@HiwayChe我添加了这个并不断得到错误。“调度程序初始化失败:无法加载配置”。你能给我举个struts.xml的例子吗?我很困惑。result\[\d+\]\.seriesSet jsonData
,jsonData
是要序列化的“root”对象,它是一个map
,result
是一个带有列表
类型值的键,这里是result\[\d+]\.seriesSet
意味着在所有“结果”列表中排除seriesSet
属性。我非常确定我已经使用带有排除参数的注释编写了结果。你只需要更努力地搜索。可能在这里:)