2

I am working on struts-rest-plugin index method to return list of my Fruit objects in xml or json format. It works really well.

Model class:

class Fruit {

    private String name;
    private double price;

    // constructor

    // getter/setter

    // equals and hash method

}

And I want to exclude some of the properties from my model object in xml/json output, say, price. I know I can wrap it with a wrapper class but it seems a bunch of things to do.

What I have tried:

@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);
    }

    ....

}

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>
...

It's not working. Please help. Thanks.

Drogba
  • 4,186
  • 4
  • 23
  • 31
  • I do not set `excludeParams` on `interceptor`. I use `excludeProperties` on `result`, you can refer to `http://struts.apache.org/release/2.2.x/docs/json-plugin.html` – hiway Apr 17 '13 at 03:47
  • @HiwayChe I add this and keep getting error. "Dispatcher initialization failed: Unable to load configuration". Could you give me a example of struts.xml? I am very confused. – Drogba Apr 17 '13 at 04:33
  • ` result\[\d+\]\.seriesSet jsonData `, `jsonData` is the 'root' object to be serialized, it is a `map`, and `result` is a key with a `list` type value, here `result\[\d+\]\.seriesSet` means exclude `seriesSet` property in all 'result' list. – hiway Apr 17 '13 at 04:52
  • I'm very sure I've written results using annotations with exclude parameters here on SO. You just need to search harder. – Quaternion Apr 17 '13 at 04:55
  • 1
    Probably here: http://stackoverflow.com/questions/4648288/problem-with-json-plugin-in-struts-2/4650575#4650575 :) – Andrea Ligios Apr 17 '13 at 07:44

1 Answers1

0

you should use XStream annotation to omit the required field from result

Piyush_Dev
  • 61
  • 1
  • 7