You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by "Vjacheslav V. Borisov" <sl...@gmail.com> on 2015/10/01 12:16:50 UTC

Consuming mutiple xml representations on resource

Hi!

I am trying to model rest api  which can consume different xml
representations, but without success
I have two methods on same url which accepts application/xml, but different
objects: single object and collection of objects

<representation mediaType="application/xml" element="cb:point"/>
<representation mediaType="application/xml" element="cb:points"/>

With two methods generated

    @POST
    @Consumes("application/xml")
    @Produces("text/plain")
    String create(Point point);

    @POST
    @Consumes("application/xml")
    @Produces("text/plain")
    String createPoints(Points points);

Becouse resource selection comes before unmarshalling, i am getting warning

12:23:44.215 [http-nio-8080-exec-11] WARN
o.apache.cxf.jaxrs.utils.JAXRSUtils - Both zzz.PointsResourceImpl#create
and zzz.PointsResourceImpl#createPoints are equal candidates for handling
the current request which can lead to unpredictable results

Does it possible or not to have different xml representations on same url
and same media type? May be this is somehow possible with simple
improvements in code, if someone points me to it.
If not, what is preferred way to implement both single and multi-object
resources?

I have options:
1) use only multiple-objects version of resource, but this is not very
elegant, always to have envelope, and many rest guides follows
single-object crud
2) use not-xml envelope, like multipart/form-data (standard, but adding
unecessary envelopes ugly)

Re: Consuming mutiple xml representations on resource

Posted by "Vjacheslav V. Borisov" <sl...@gmail.com>.
2015-10-04 20:25 GMT+04:00 Andrei Shakirin <as...@talend.com>:

>
> You have the following options:
> 1. Leave only single-object version with a POST. It makes easy to return
> URI of created resource in Location header or single error code in fault
> case.
> If Option (1) is not acceptable because of performance reason, I would
> suggest either to
> 2. Introduce own media type (like application/point.collection+xml) and
> use POST to add all resources to the same URI. In this case you have to
> deal with multiple created URIs (return them to client directly or via
> result resource) and multiple error codes in fault case.
> 3. Introduce PUT to replace the whole collection. In this case client
> should be responsible to specify individual resource URIs. You have to deal
> with multiple error codes in fault case.
>

Thanks, I will use PUT as workaround before PATCH will be available in
client and server



2015-10-04 20:25 GMT+04:00 Andrei Shakirin <as...@talend.com>:

> Hi,
>
> The warning message is correct, because accordingly JAX-RS spec, the
> method must be selected based on URI, HTTP Verb, Request MediaType and
> Response Accepted Type:
>
> " 3.7.2 Request Matching
> A request is matched to the corresponding resource method or sub-resource
> method by comparing the normalized
> request URI (see Section 3.7.1), the media type of any request entity, and
> the requested response
> entity format to the metadata annotations on the resource classes and
> their methods. "
>
> You have the following options:
> 1. Leave only single-object version with a POST. It makes easy to return
> URI of created resource in Location header or single error code in fault
> case.
> If Option (1) is not acceptable because of performance reason, I would
> suggest either to
> 2. Introduce own media type (like application/point.collection+xml) and
> use POST to add all resources to the same URI. In this case you have to
> deal with multiple created URIs (return them to client directly or via
> result resource) and multiple error codes in fault case.
> 3. Introduce PUT to replace the whole collection. In this case client
> should be responsible to specify individual resource URIs. You have to deal
> with multiple error codes in fault case.
>
> Regards,
> Andrei.
>
> > -----Original Message-----
> > From: Vjacheslav V. Borisov [mailto:slavb18@gmail.com]
> > Sent: Donnerstag, 1. Oktober 2015 15:35
> > To: users@cxf.apache.org
> > Subject: Re: Consuming mutiple xml representations on resource
> >
> > 2015-10-01 14:16 GMT+04:00 Vjacheslav V. Borisov <sl...@gmail.com>:
> >
> > > Hi!
> > >
> > > I am trying to model rest api  which can consume different xml
> > > representations, but without success I have two methods on same url
> > > which accepts application/xml, but different objects: single object
> > > and collection of objects
> > >
> > >
> >
> http://stackoverflow.com/questions/411462/restful-way-to-create-multiple-
> > items-in-one-request
> >
> > Another option is to use different method, like PUT (but PUT is like
> replacing
> > collection), or PATCH  (modify collection) Unfortunately wadl2java do not
> > generate @PATCH annotation for PATCH methods
>

RE: Consuming mutiple xml representations on resource

Posted by Andrei Shakirin <as...@talend.com>.
Hi,

The warning message is correct, because accordingly JAX-RS spec, the method must be selected based on URI, HTTP Verb, Request MediaType and Response Accepted Type:

" 3.7.2 Request Matching
A request is matched to the corresponding resource method or sub-resource method by comparing the normalized
request URI (see Section 3.7.1), the media type of any request entity, and the requested response
entity format to the metadata annotations on the resource classes and their methods. "

You have the following options:
1. Leave only single-object version with a POST. It makes easy to return URI of created resource in Location header or single error code in fault case.
If Option (1) is not acceptable because of performance reason, I would suggest either to
2. Introduce own media type (like application/point.collection+xml) and use POST to add all resources to the same URI. In this case you have to deal with multiple created URIs (return them to client directly or via result resource) and multiple error codes in fault case.
3. Introduce PUT to replace the whole collection. In this case client should be responsible to specify individual resource URIs. You have to deal with multiple error codes in fault case.

Regards,
Andrei.

> -----Original Message-----
> From: Vjacheslav V. Borisov [mailto:slavb18@gmail.com]
> Sent: Donnerstag, 1. Oktober 2015 15:35
> To: users@cxf.apache.org
> Subject: Re: Consuming mutiple xml representations on resource
> 
> 2015-10-01 14:16 GMT+04:00 Vjacheslav V. Borisov <sl...@gmail.com>:
> 
> > Hi!
> >
> > I am trying to model rest api  which can consume different xml
> > representations, but without success I have two methods on same url
> > which accepts application/xml, but different objects: single object
> > and collection of objects
> >
> >
> http://stackoverflow.com/questions/411462/restful-way-to-create-multiple-
> items-in-one-request
> 
> Another option is to use different method, like PUT (but PUT is like replacing
> collection), or PATCH  (modify collection) Unfortunately wadl2java do not
> generate @PATCH annotation for PATCH methods

Re: Consuming mutiple xml representations on resource

Posted by "Vjacheslav V. Borisov" <sl...@gmail.com>.
2015-10-01 14:16 GMT+04:00 Vjacheslav V. Borisov <sl...@gmail.com>:

> Hi!
>
> I am trying to model rest api  which can consume different xml
> representations, but without success
> I have two methods on same url which accepts application/xml, but
> different objects: single object and collection of objects
>
>
http://stackoverflow.com/questions/411462/restful-way-to-create-multiple-items-in-one-request

Another option is to use different method, like PUT (but PUT is like
replacing collection), or PATCH  (modify collection)
Unfortunately wadl2java do not generate @PATCH annotation for PATCH methods

Re: Consuming mutiple xml representations on resource

Posted by Sergey Beryozkin <sb...@gmail.com>.
I think in earlier versions if you had only aplication/json with an 
element ref then it would still not generate a class like Session/etc.
But now both application/json and application/xml with element refs will 
generate it.

Next, some users want to have say multiple XML representations but 
pointing to different schema elements - this is a primary reason why 
JAXP Source is generated in such cases.

However, there is a supportMultipleXmlReps parameter - this will need to 
be deprecated and renamed now that application/json refs are also 
checked, so use it and you'd get multiple methods generated - example, 
you'd have

editXml(Session)
editJson(Session)

This in itself is new - now the proxies can select which format they 
want to use - before, with only edit(Session) - it would always be the 
format of the 1st representation - say, always XML.

However I know that there was some pushback and some users still want to 
have edit(Session) even with multiple representations. So I'll spend 
some time on making the old generation style supported. However, the use 
of 'supportMultipleXmlReps' is needed from now on in such cases

Cheers, Sergey


On 06/10/15 11:35, Vjacheslav V. Borisov wrote:
> 2015-10-05 18:28 GMT+04:00 Sergey Beryozkin <sb...@gmail.com>:
>
>> Done for 3.1.4-SNAPSHOT
>>
>
> Thanks again, we will try it. Upgrading from 3.0.4 found strange behavior
> (started at 3.0.6)
> When method request contains xml and json representation and both contain
> "element" reference
>                  <method name="PUT" id="edit">
>                      <request>
>                          <representation mediaType="application/xml"
> element="cb:session"/>
>                          <representation mediaType="application/json"
> element="cb:session"/>
>                      </request>
>                  </method>
>
> wadl2java generates perameter with javax.xml.transform.Source istead of
> Session
>
> but when i remove element on json representation, it generates element of
> Session, so after upgrading need to remove element attribute on json
> representations
>
>                  <method name="PUT" id="edit">
>                      <request>
>                          <representation mediaType="application/xml"
> element="cb:session"/>
>                          <representation mediaType="application/json"/>
>                      </request>
>                  </method>
>
> does this change made on purpose?
>


Re: Consuming mutiple xml representations on resource

Posted by "Vjacheslav V. Borisov" <sl...@gmail.com>.
2015-10-05 18:28 GMT+04:00 Sergey Beryozkin <sb...@gmail.com>:

> Done for 3.1.4-SNAPSHOT
>

Thanks again, we will try it. Upgrading from 3.0.4 found strange behavior
(started at 3.0.6)
When method request contains xml and json representation and both contain
"element" reference
                <method name="PUT" id="edit">
                    <request>
                        <representation mediaType="application/xml"
element="cb:session"/>
                        <representation mediaType="application/json"
element="cb:session"/>
                    </request>
                </method>

wadl2java generates perameter with javax.xml.transform.Source istead of
Session

but when i remove element on json representation, it generates element of
Session, so after upgrading need to remove element attribute on json
representations

                <method name="PUT" id="edit">
                    <request>
                        <representation mediaType="application/xml"
element="cb:session"/>
                        <representation mediaType="application/json"/>
                    </request>
                </method>

does this change made on purpose?

Re: Consuming mutiple xml representations on resource

Posted by Sergey Beryozkin <sb...@gmail.com>.
Done for 3.1.4-SNAPSHOT

Sergey
On 05/10/15 08:30, Vjacheslav V. Borisov wrote:
> 2015-10-05 0:56 GMT+04:00 Sergey Beryozkin <sb...@gmail.com>:
>
>>
>> Re Patch/etc - yes wadl-to-java can not generate the custom annotation
>> classes - it is time to do it though, will take care of it
>>
>
> Thanks !
>


-- 
Sergey Beryozkin

Talend Community Coders
http://coders.talend.com/

Re: Consuming mutiple xml representations on resource

Posted by "Vjacheslav V. Borisov" <sl...@gmail.com>.
2015-10-05 0:56 GMT+04:00 Sergey Beryozkin <sb...@gmail.com>:

>
> Re Patch/etc - yes wadl-to-java can not generate the custom annotation
> classes - it is time to do it though, will take care of it
>

Thanks !

Re: Consuming mutiple xml representations on resource

Posted by Sergey Beryozkin <sb...@gmail.com>.
Hi

The method parameters do not affect the selection algorithm.
You can try a custom CXF ResourceComparator - then you can tell the 
runtime which of the two equal candidates wins.

Re Patch/etc - yes wadl-to-java can not generate the custom annotation 
classes - it is time to do it though, will take care of it

Sergey
On 01/10/15 11:16, Vjacheslav V. Borisov wrote:
> Hi!
>
> I am trying to model rest api  which can consume different xml
> representations, but without success
> I have two methods on same url which accepts application/xml, but different
> objects: single object and collection of objects
>
> <representation mediaType="application/xml" element="cb:point"/>
> <representation mediaType="application/xml" element="cb:points"/>
>
> With two methods generated
>
>      @POST
>      @Consumes("application/xml")
>      @Produces("text/plain")
>      String create(Point point);
>
>      @POST
>      @Consumes("application/xml")
>      @Produces("text/plain")
>      String createPoints(Points points);
>
> Becouse resource selection comes before unmarshalling, i am getting warning
>
> 12:23:44.215 [http-nio-8080-exec-11] WARN
> o.apache.cxf.jaxrs.utils.JAXRSUtils - Both zzz.PointsResourceImpl#create
> and zzz.PointsResourceImpl#createPoints are equal candidates for handling
> the current request which can lead to unpredictable results
>
> Does it possible or not to have different xml representations on same url
> and same media type? May be this is somehow possible with simple
> improvements in code, if someone points me to it.
> If not, what is preferred way to implement both single and multi-object
> resources?
>
> I have options:
> 1) use only multiple-objects version of resource, but this is not very
> elegant, always to have envelope, and many rest guides follows
> single-object crud
> 2) use not-xml envelope, like multipart/form-data (standard, but adding
> unecessary envelopes ugly)
>


-- 
Sergey Beryozkin

Talend Community Coders
http://coders.talend.com/