Hi heikki!

I needed the same so I did following:

extending AbstractResource
implemented the needed:

protected ResourceResponse newResourceResponse(Attributes attributes) {
  ResourceResponse resourceResponse = new ResourceResponse();
  final StringBuilder sb = new StringBuilder();
  //do sb.append() here your json etc.
  resourceResponse.setWriteCallback(new WriteCallback() {
            @Override
public void writeData(Attributes attributes) throws IOException {
                Response response = attributes.getResponse();
                response.write(sb.toString());
            }
        });

        resourceResponse.setContentType("application/json");
        return resourceResponse;
}

written a subclass of ResourceReference which takes a Class as constructor parameter.
@Martin: why doesn't wicket hasn't something generic?

public class GenericResourceReference extends ResourceReference {
    private Class<? extends IResource> iResourceClass;

public GenericResourceReference(Class<? extends IResource> iResourceClass) {
        super(iResourceClass.getName());
        this.iResourceClass = iResourceClass;
    }

    @Override
    public IResource getResource() {
        try {
            return iResourceClass.newInstance();
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

and mounted it in the WebApplication init() method like this:
mountResource("/my.json", new GenericResourceReference(MyResource.class));

Greetings from sunny Austria.
-Wolfgang

On 04/25/2013 04:28 PM, heikki wrote:
thanks,

that example does indeed work fine, but I'd rather have *no* markup file,
just generate the XML or JSON myself and send that back in the response.

So I need a mounted IResource to do that ? Any tip how to go about it for
this use case, e.g. use a ByteArrayResource ?




--
View this message in context: 
http://apache-wicket.1842946.n4.nabble.com/Wicket-returning-XML-or-JSON-tp4658271p4658273.html
Sent from the Users forum mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]


Reply via email to