You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by Tom <tb...@tbee.org> on 2009/05/12 14:49:33 UTC

making domain objects available to the client

So I have been able to code my service using JAX-RS and I get propery 
XML back and can send it. All peachy.

Now, once the XML is on the client side, I need to reconstruct the 
objects that were transferred.

            JAXBContext jc = JAXBContext.newInstance( User.class );
            Unmarshaller u = jc.createUnmarshaller();
            User lUser = (User)u.unmarshal(new ByteArrayInputStream( 
s.getBytes() ) );
            System.out.println(lUser);


Ok there too. So.

Then it got me thinking... That unmarshalled object is using the 
original class definition that was marshalled before (this is inside a 
unittest, so both the server and the unittest access the same User 
class). But I do not want my client to use my original class; these are 
fairly complex classes which are interlaced with JPA code. For example 
methods like "getGroups()" which required new entities which may not 
have been send as XML. What I really would require is a shell-class that 
only contains the stuff that JAXB has (un)marshalled, but is "XML 
compatible" with the actual business class.

I know that JAXB can create Java classes based on an XML-Schema, but I 
do not have an XML schema, only annotated classes, and I do not like to 
have duplication.

1. Is there a way to convert annotated classes to an XML-schema so that 
can be used to generated shell-classes?

2. does anyone have a suggestion on how else to handle this 
don't-want-my-domain-objects-made-available-to-the-client?

Tom


Re: making domain objects available to the client

Posted by Tom <tb...@tbee.org>.
Yes. I found the schemagen in the JAXB tools, also available as an ANT 
task. And then one can use the schema to generate shell-classes.

However, the tool is not quite working as expected; it keeps generating 
a file "schema2.xsd" and I want a different name :-) So that will 
require some more work.

Thanks for the suggestion!

Tom




Daniel Kulp wrote:
> The JAXBContext has a generateSchema method that can be used to tell it to 
> generate Schemas for the classes it knows about.   Something like:
>
> final List<DOMResult> results = new ArrayList<DOMResult>();
>
>         context.generateSchema(new SchemaOutputResolver() {
>             @Override
>             public Result createOutput(String ns, String file) throws 
> IOException {
>                 DOMResult result = new DOMResult();
>                 result.setSystemId(file);
>                 results.add(result);
>                 return result;
>             }
>         });
>
> would give you the List of DOMs for the schemas.   You could then save them 
> out or something and use them to generate new code.
>
> Dan
>
>
>
>
> On Tue May 12 2009 8:49:33 am Tom wrote:
>   
>> So I have been able to code my service using JAX-RS and I get propery
>> XML back and can send it. All peachy.
>>
>> Now, once the XML is on the client side, I need to reconstruct the
>> objects that were transferred.
>>
>>             JAXBContext jc = JAXBContext.newInstance( User.class );
>>             Unmarshaller u = jc.createUnmarshaller();
>>             User lUser = (User)u.unmarshal(new ByteArrayInputStream(
>> s.getBytes() ) );
>>             System.out.println(lUser);
>>
>>
>> Ok there too. So.
>>
>> Then it got me thinking... That unmarshalled object is using the
>> original class definition that was marshalled before (this is inside a
>> unittest, so both the server and the unittest access the same User
>> class). But I do not want my client to use my original class; these are
>> fairly complex classes which are interlaced with JPA code. For example
>> methods like "getGroups()" which required new entities which may not
>> have been send as XML. What I really would require is a shell-class that
>> only contains the stuff that JAXB has (un)marshalled, but is "XML
>> compatible" with the actual business class.
>>
>> I know that JAXB can create Java classes based on an XML-Schema, but I
>> do not have an XML schema, only annotated classes, and I do not like to
>> have duplication.
>>
>> 1. Is there a way to convert annotated classes to an XML-schema so that
>> can be used to generated shell-classes?
>>
>> 2. does anyone have a suggestion on how else to handle this
>> don't-want-my-domain-objects-made-available-to-the-client?
>>
>> Tom
>>     
>
>   


Re: making domain objects available to the client

Posted by Daniel Kulp <dk...@apache.org>.
The JAXBContext has a generateSchema method that can be used to tell it to 
generate Schemas for the classes it knows about.   Something like:

final List<DOMResult> results = new ArrayList<DOMResult>();

        context.generateSchema(new SchemaOutputResolver() {
            @Override
            public Result createOutput(String ns, String file) throws 
IOException {
                DOMResult result = new DOMResult();
                result.setSystemId(file);
                results.add(result);
                return result;
            }
        });

would give you the List of DOMs for the schemas.   You could then save them 
out or something and use them to generate new code.

Dan




On Tue May 12 2009 8:49:33 am Tom wrote:
> So I have been able to code my service using JAX-RS and I get propery
> XML back and can send it. All peachy.
>
> Now, once the XML is on the client side, I need to reconstruct the
> objects that were transferred.
>
>             JAXBContext jc = JAXBContext.newInstance( User.class );
>             Unmarshaller u = jc.createUnmarshaller();
>             User lUser = (User)u.unmarshal(new ByteArrayInputStream(
> s.getBytes() ) );
>             System.out.println(lUser);
>
>
> Ok there too. So.
>
> Then it got me thinking... That unmarshalled object is using the
> original class definition that was marshalled before (this is inside a
> unittest, so both the server and the unittest access the same User
> class). But I do not want my client to use my original class; these are
> fairly complex classes which are interlaced with JPA code. For example
> methods like "getGroups()" which required new entities which may not
> have been send as XML. What I really would require is a shell-class that
> only contains the stuff that JAXB has (un)marshalled, but is "XML
> compatible" with the actual business class.
>
> I know that JAXB can create Java classes based on an XML-Schema, but I
> do not have an XML schema, only annotated classes, and I do not like to
> have duplication.
>
> 1. Is there a way to convert annotated classes to an XML-schema so that
> can be used to generated shell-classes?
>
> 2. does anyone have a suggestion on how else to handle this
> don't-want-my-domain-objects-made-available-to-the-client?
>
> Tom

-- 
Daniel Kulp
dkulp@apache.org
http://www.dankulp.com/blog