struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jane Wayne <jane.wayne2...@gmail.com>
Subject Re: how to write json/xml output using struts 2 and rest + convention plugin
Date Fri, 05 Oct 2012 14:03:24 GMT
yes, somewhat (not xml, but json). i modified the rest showcase. in
particular, i modified, orders-editNew.jsp. i modified the form the
look like this:

<s:form method="post" action="%{#request.contextPath}/orders.json">

the original line was:

<s:form method="post" action="%{#request.contextPath}/orders">

i believe the place to return XML/JSON is in the method create().

    // POST /orders
    public HttpHeaders create() {
        ordersService.save(model);
        addActionMessage("New order created successfully");
        return new DefaultHttpHeaders("success")
            .setLocationId(model.getId());
    }

this method returns a new HttpHeaders object signalling "success", and
in this showcase, "success" gets map back to index() which is GET
/orders (shows all the orders). but these are not response output (as
in request/response).

the struts2-rest-plugin tutorial goes to explain the mapping of GET,
POST, PUT, DELETE to default controller methods
(http://struts.apache.org/2.x/docs/rest-plugin.html). however, it is
not clear to me, what i can return for the method. in fact, it seems
that a result of type HttpHeaders or String can be returned (the
difference is explained, and very helpful).

when i used the struts2-json-plugin, all i remember doing was adding
@ParentPackage(value="json-default") to the action class + adding
@Action { ... @Result(...,type="json",params={"root","myPojo"}) } to
the method. automagically, myPojo was serialized to JSON.

i'm wondering if it's even possible to do the same with struts2 +
convention + rest ?

before using the struts2-json-plugin, my action would extend
ActionSupport and implement ServletRequestAware and
ServletResponseAware. i would then simply use ServletOutputStream to
write the JSON/XML stuff directly. it's not elegant, but i might
kludge an approach like this one.

On Fri, Oct 5, 2012 at 8:22 AM, Lukasz Lenart <lukaszlenart@apache.org> wrote:
> 2012/10/5 Jane Wayne <jane.wayne2978@gmail.com>:
>> not really/entirely what i'm looking for. the struts2-json-plugin
>> supports json. but what about xml? i need the flexibility to alter
>> between the two for the client/consumer of the rest services.
>
> Did you try to add .xml suffix to request action ?
>
>
> Regards
>
> --
> Ɓukasz
> + 48 606 323 122 http://www.lenart.org.pl/
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Mime
View raw message