You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by laksu <le...@leventaksu.com> on 2011/11/29 18:14:09 UTC

style sheet

Hi,
I am new to JAXB, JAX-WS, JAX-RS and Apache CXF as well. Still I have been
able to successfully publish a service of mine as web services and RESTful
web service using CXF. Amazing.
My approach was having my Java service designed first and annotated my
classes with JAXB and let the WSDL generated by CXF.

@GET
@Produces({MediaType.APPLICATION_XML,MediaType.TEXT_XML,MediaType.TEXT_HTML}) 
@Path("listProducts")
public List<Product> listProducts() {...

That works quite well and gets me the XML I wanted:

<products>
    <product id="1658" digitalId="5099972936955" title="Les lépidoptères"
displayArtist="Jean-Louis Aubert">
        <availabilities>
             <availability territoryCode="GB" product="1658"
streamingDate="2011-05-07T00:00:00+01:00"
salesStartDate="2011-05-02T00:00:00+01:00">
                  <id>15350</id>
             </availability>
             <availability territoryCode="AE" product="1658"
streamingDate="2011-09-05T00:00:00+01:00"
salesStartDate="2011-10-03T00:00:00+01:00">
                  <id>15352</id>
             </availability>
...
        <availabilities>
    <product id=...&lt;/product>
...
</products>


I am using Excel to get the data from this RESTful web service method and
display to the user. (Excel-Data-Get external data from Web). That is
because the user will be free to manipulate it further (filter, sort, chart,
etc.)  However, Excel does some approximation on the data types and it does
not work correctly with every field. For example dates are imported as text
fields and if a field is comprised of digits then it is imported as a number
field. I definitely need an XSD to accompany the generated XML files.
I wonder if that schema can be generated automatically, either on the fly or
during build?
Thanks.


--
View this message in context: http://cxf.547215.n5.nabble.com/style-sheet-tp5033048p5033048.html
Sent from the cxf-user mailing list archive at Nabble.com.

Re: style sheet

Posted by Sergey Beryozkin <sb...@gmail.com>.
On 08/12/11 03:55, laksu wrote:
> Sounds great.
> I have wrapped all the required output into a single custom class using
> subclasses as necessary. It now generates the xsd at wadl query. This also
> optimizes bandwidth by omitting unnecessary fields.
>
> The url:
>
> http://pathToMyMethod?_wadl&_type=xml
>
> returns the xsd I require, but it is wrapped in<application><grammar>
> elements. If I can get the plain xsd with wadl queries, along with the
> @XSISchemaLocation annotation, in that case that will be the perfect
> solution.

As I said earlier on, WADL is of no use in this case as Excel can't 
understand it, you need to get that schema available as a resource which 
xsi:schemaLocation or xsi:noNamespaceSchemaLocation pointing to it. 
Extract the generated schema from WADL and save it, say to, 
schemas/products.xsd, which is relative to the root of the web 
application, and then use XSISchemaLocation to refer to it using that 
relative path:

https://cwiki.apache.org/confluence/display/CXF20DOC/JAX-RS+Advanced+XML#JAX-RSAdvancedXML-XSISchemaLocation



>
> I don't get the JAXbElementProvider part. Or is it something related with
> controlling the wadl generation?
Please see the above link, it's to do with affecting the absolute 
reference to a given schema resource

Cheers, Sergey
>
> Thank you very much
>
> --
> View this message in context: http://cxf.547215.n5.nabble.com/automatic-schema-generation-for-a-RS-method-tp5033048p5057660.html
> Sent from the cxf-user mailing list archive at Nabble.com.


-- 
Sergey Beryozkin

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

Blog: http://sberyozkin.blogspot.com

Re: style sheet

Posted by laksu <le...@leventaksu.com>.
Sounds great.
I have wrapped all the required output into a single custom class using
subclasses as necessary. It now generates the xsd at wadl query. This also
optimizes bandwidth by omitting unnecessary fields.

The url:

http://pathToMyMethod?_wadl&_type=xml

returns the xsd I require, but it is wrapped in <application><grammar>
elements. If I can get the plain xsd with wadl queries, along with the
@XSISchemaLocation annotation, in that case that will be the perfect
solution.

I don't get the JAXbElementProvider part. Or is it something related with
controlling the wadl generation?

Thank you very much

--
View this message in context: http://cxf.547215.n5.nabble.com/automatic-schema-generation-for-a-RS-method-tp5033048p5057660.html
Sent from the cxf-user mailing list archive at Nabble.com.

Re: style sheet

Posted by Sergey Beryozkin <sb...@gmail.com>.
On 02/12/11 17:15, laksu wrote:
> I am using XMLSpy for playing with XMLs and XSDs.
>
> The following is supplied by XMLSpy into the root element of my XML, when I
> choose "Assign Schema" from its menu:
>
> <products xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
> xsi:noNamespaceSchemaLocation="listProducts.xsd">
>     <product id="1658" ...
>
> and my XSD starts like that:
>
> <?xml version="1.0" encoding="UTF-8"?>
> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
> 	<xs:element name="products">
> 		<xs:complexType>....
>
> I got that by asking XMLSpy to generate.

I added XSISchemaLocation annotation which you can add to a given 
resource method and reference a schema using a relative path, example:
@XSISchemaLocation("schemas/products.xsd")

This will be resolved against a web application context path.
It is also possible to hide "schemas" part by additionally configuring 
JAXbElementProvider, I'll send a link to the docs a bit later on

Cheers, Sergey


> Thanks,
> Levent
>
> --
> View this message in context: http://cxf.547215.n5.nabble.com/automatic-schema-generation-for-a-RS-method-tp5033048p5042635.html
> Sent from the cxf-user mailing list archive at Nabble.com.


-- 
Sergey Beryozkin

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

Blog: http://sberyozkin.blogspot.com

Re: style sheet

Posted by laksu <le...@leventaksu.com>.
I am using XMLSpy for playing with XMLs and XSDs.

The following is supplied by XMLSpy into the root element of my XML, when I
choose "Assign Schema" from its menu:

<products xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="listProducts.xsd">
   <product id="1658" ...

and my XSD starts like that:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
	<xs:element name="products">
		<xs:complexType>....

I got that by asking XMLSpy to generate.
Thanks,
Levent

--
View this message in context: http://cxf.547215.n5.nabble.com/automatic-schema-generation-for-a-RS-method-tp5033048p5042635.html
Sent from the cxf-user mailing list archive at Nabble.com.

Re: style sheet

Posted by Sergey Beryozkin <sb...@gmail.com>.
Hi
On 02/12/11 15:00, laksu wrote:
> Ok,
> It took me some while to figure out on a few things. I am still quick when
> you consider I am nil about XSDs.
>
> Here are my findinds:
> - Excel uses xsd information if it is supplied.
> - Excel uses the same mechanism when it is asked to open an XML file. So in
> that case it does not have to go through the Web server. That simplifies
> with testing.
> - xs:string in xsd works well with a number-only field without further
> intervention. It is imported into Excel with  single quote prefix.
> - xs:dateTime does not work right out of the box. Because Excel rejects a
> number of certain formats. The following link tells about those formats:
> http://office.microsoft.com/en-us/excel-help/xml-schema-definition-xsd-data-type-support-HP001046225.aspx
> http://office.microsoft.com/en-us/excel-help/xml-schema-definition-xsd-data-type-support-HP001046225.aspx
> It tells about the "unsupported" formats. Maybe it is me who is that naive
> to look for it but I could not find something telling about "supported"
> ones. In that case, I tried removing the time zone info from the end and
> then Excel recognized that very well as a date cell.
>
> Excel recognizes: streamingDate="2011-09-07T00:00:00"
> but takes the following as string:
> salesStartDate="2011-10-02T00:00:00+01:00"
> I imagine Excel does not associate time zone info in its date cells so it
> refuses those formats.
>
> So, by manually hard-coding the xsd schema and tweaking the dateTime output
> with a custom "Adapter" Excel imports the result XML flawlessly.
>
> I will be trying the next steps.
Yes please, check where it expects to get an xsd info from, I'm nearly 
sure it expects an xsi:schemaLocation attribute - if you can confirm by 
early next week then I might be able to sqeeze a SchemaLocation 
annotation in time for 2.5.1

Sergey

> Thanks.
>
> --
> View this message in context: http://cxf.547215.n5.nabble.com/automatic-schema-generation-for-a-RS-method-tp5033048p5042117.html
> Sent from the cxf-user mailing list archive at Nabble.com.


-- 
Sergey Beryozkin

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

Blog: http://sberyozkin.blogspot.com

Re: style sheet

Posted by laksu <le...@leventaksu.com>.
Ok,
It took me some while to figure out on a few things. I am still quick when
you consider I am nil about XSDs.

Here are my findinds:
- Excel uses xsd information if it is supplied.
- Excel uses the same mechanism when it is asked to open an XML file. So in
that case it does not have to go through the Web server. That simplifies
with testing.
- xs:string in xsd works well with a number-only field without further
intervention. It is imported into Excel with  single quote prefix.
- xs:dateTime does not work right out of the box. Because Excel rejects a
number of certain formats. The following link tells about those formats:
http://office.microsoft.com/en-us/excel-help/xml-schema-definition-xsd-data-type-support-HP001046225.aspx
http://office.microsoft.com/en-us/excel-help/xml-schema-definition-xsd-data-type-support-HP001046225.aspx 
It tells about the "unsupported" formats. Maybe it is me who is that naive
to look for it but I could not find something telling about "supported"
ones. In that case, I tried removing the time zone info from the end and
then Excel recognized that very well as a date cell.

Excel recognizes: streamingDate="2011-09-07T00:00:00"
but takes the following as string:
salesStartDate="2011-10-02T00:00:00+01:00"
I imagine Excel does not associate time zone info in its date cells so it
refuses those formats. 

So, by manually hard-coding the xsd schema and tweaking the dateTime output
with a custom "Adapter" Excel imports the result XML flawlessly.

I will be trying the next steps.
Thanks.

--
View this message in context: http://cxf.547215.n5.nabble.com/automatic-schema-generation-for-a-RS-method-tp5033048p5042117.html
Sent from the cxf-user mailing list archive at Nabble.com.

Re: style sheet

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

it appears to be quite an interesting case that may lead us to doing a 
minor enhancement,

On 01/12/11 12:30, laksu wrote:
> Hi Sergey, thanks for the reply.
> Excel has a concept called an "XML Mapping". Upon connecting to my RS method
> I get this
> "The specified XML source does not refer to a schema. Excel will create a
> schema based on the source XML data"

It appears Excel expects something like this:
<products xmlns="http://myproducts"
   xsi:schemaLocation="
http://myproducts http://localhost:8080/mywebapp/schemas/products.xsd">
<!-- products data -->
</products>

Can you please confirm it by importing an xml document locally and with 
xsi:schemaLocation pointing to a local file system, ex, 
"file:/C:/schemas/products.xsd' ? Or may be check some of their tech 
notes...

> would automatically pick it up. That would be nice to have but I would be
> even fine with supplying the XSD manually to Excel. Essentially at this
> stage I am interested in generating the XSD automatically and supplying it
> to Excel would be another thing.
> What I understand from your reply is if I had been returning a single object
> rather than a collection, then it would be possible to generate the schema
> definition. I think this does not work right out of the box and I should be
> doing something for that as I have another method that is returning a single
> object but it is returning a similar plain XML file with no reference to any
> XSD.

If you wrap it in a single bean then you can query the service for a 
WADL instance (using a ?_wadl query) and that WADL will include the 
generated schema, WADL itself will be of no use in this particular case 
but you can copy the generated schema and reuse it

Assuming Excel does expect a xsi:schemaLocation set, I think what you 
need to do is to get the schema created (manually or copy it from the 
generated WADL) and save it to say /schemas/products.xsd relative to the 
webapp root.

Next, configure JAXBElementProvider as shown here:

http://cxf.apache.org/docs/jax-rs-data-bindings.html#JAX-RSDataBindings-ConfiguringJAXBprovider, 
set a property like this
<entry key="jaxb.schemaLocation" "http://myproducts /schemas/products.xsd"/>

Now, if Excel will manage to resolve this relative 
"/schemas/products.xsd" against the original request URI then things are 
good.

If not then you can confirm things are working by hardcoding an absolute 
location:

<entry key="jaxb.schemaLocation" "http://myproducts 
http://localhost:8080/mywebapp/schemas/products.xsd"/>

but to fix it properly, we'd need to probably introduce a SchemaLocation 
annotation and may be manage some substitution there, similarly to a 
would be XMLInstruction annotation which Christian said would be handy 
to have;

Extending JAXBElementProvider will let you manage the absolute URI for 
this schemaLocation easily enough too, but perhaps @SchemaLocation would 
be even easier

Let me know please how it works
Cheers, Sergey
>
>
> --
> View this message in context: http://cxf.547215.n5.nabble.com/automatic-schema-generation-for-a-RS-method-tp5033048p5038521.html
> Sent from the cxf-user mailing list archive at Nabble.com.


-- 
Sergey Beryozkin

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

Blog: http://sberyozkin.blogspot.com

Re: style sheet

Posted by laksu <le...@leventaksu.com>.
Hi Sergey, thanks for the reply.
Excel has a concept called an "XML Mapping". Upon connecting to my RS method
I get this
"The specified XML source does not refer to a schema. Excel will create a
schema based on the source XML data"
But the guesswork does not work very well every time.
After import the user is able to change the XML Map by supplying an
alternative XSD.
I suppose if the response XML had the schema definition in header then Excel
would automatically pick it up. That would be nice to have but I would be
even fine with supplying the XSD manually to Excel. Essentially at this
stage I am interested in generating the XSD automatically and supplying it
to Excel would be another thing.
What I understand from your reply is if I had been returning a single object
rather than a collection, then it would be possible to generate the schema
definition. I think this does not work right out of the box and I should be
doing something for that as I have another method that is returning a single
object but it is returning a similar plain XML file with no reference to any
XSD.


--
View this message in context: http://cxf.547215.n5.nabble.com/automatic-schema-generation-for-a-RS-method-tp5033048p5038521.html
Sent from the cxf-user mailing list archive at Nabble.com.

Re: style sheet

Posted by Sergey Beryozkin <sb...@gmail.com>.
Hi, please see comments inline

On 29/11/11 17:14, laksu wrote:
> Hi,
> I am new to JAXB, JAX-WS, JAX-RS and Apache CXF as well. Still I have been
> able to successfully publish a service of mine as web services and RESTful
> web service using CXF. Amazing.
super :-)

> My approach was having my Java service designed first and annotated my
> classes with JAXB and let the WSDL generated by CXF.
>
> @GET
> @Produces({MediaType.APPLICATION_XML,MediaType.TEXT_XML,MediaType.TEXT_HTML})
> @Path("listProducts")
> public List<Product>  listProducts() {...
>
> That works quite well and gets me the XML I wanted:
>
> <products>
>      <product id="1658" digitalId="5099972936955" title="Les lépidoptères"
> displayArtist="Jean-Louis Aubert">
>          <availabilities>
>               <availability territoryCode="GB" product="1658"
> streamingDate="2011-05-07T00:00:00+01:00"
> salesStartDate="2011-05-02T00:00:00+01:00">
>                    <id>15350</id>
>               </availability>
>               <availability territoryCode="AE" product="1658"
> streamingDate="2011-09-05T00:00:00+01:00"
> salesStartDate="2011-10-03T00:00:00+01:00">
>                    <id>15352</id>
>               </availability>
> ...
>          <availabilities>
>      <product id=...&lt;/product>
> ...
> </products>
>
>
> I am using Excel to get the data from this RESTful web service method and
> display to the user. (Excel-Data-Get external data from Web). That is
> because the user will be free to manipulate it further (filter, sort, chart,
> etc.)  However, Excel does some approximation on the data types and it does
> not work correctly with every field. For example dates are imported as text
> fields and if a field is comprised of digits then it is imported as a number
> field. I definitely need an XSD to accompany the generated XML files.
> I wonder if that schema can be generated automatically, either on the fly or
> during build?

How do you expect this schema be processed on the client side ? Would 
you like it included with the "products" payload itself or happy to see 
it referenced inside a WADL document ?
I'd recommend wrapping List<Product> in a Products bean - it is the only 
way at the moment for the JAX-RS runtime to auto-generate a schema.

Now, if the tool which is expecting these data back understands WSDL 
only then one option is to create such a WSDL upfront, generate a JAXWS 
code from it and then apply an external JAX-RS user model to it, but may 
be that won't be needed

Cheers, Sergey

> Thanks.
>
>
> --
> View this message in context: http://cxf.547215.n5.nabble.com/style-sheet-tp5033048p5033048.html
> Sent from the cxf-user mailing list archive at Nabble.com.


-- 
Sergey Beryozkin

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

Blog: http://sberyozkin.blogspot.com