Using Jax-B with Jax-WS in order to return complex types
I'm sure you get plenty of questions on this type of thing on here, but none of the answers I've found seem to deal with the problem I'm having, so I was hoping that I might be able to get some specific answers.
I have a project which consists of a client and a server. The server connects to a database and the client can call the methods in the server through the wsdl file. This works fine, but the server can only return simple types and lists. I looked up how to handle custom types, and I was directed to JaxB, which seems to be exactly what I'm looking for. Unfortunately I'm a little confused as to how I'm supposed to use it. I'm using the NetBeans IDE (6.9.1), which makes it easier to deal with the wsdl files, although I have read that passing complex classes is fairly trivial from the command line, once you've got the wsdl end of things working properly?
What I want to do is to have the server return a class - it'll just contain data - that the client can read and use.
I've开发者_Python百科 done the following, but obviously I'm going wrong somewhere!
- I have a class, called Customer on the server side which is constructed and returned when a particular method is called by the client.
- The client obviously cannot interpret this class correctly.
- I've used schemagen to create an XML schema from the Customer class. I then use the netbeans wizard to bind the schema to a class in the client.
- I suppose this is where I'm confused. I want to unmarshall the xml response from the server and use it to create a new object on the client. I'm using the "jaxbu" netbeans shortcut which expands into some code, but I'm unsure of how to put the returning object into the unmarshaller. If that makes any sense?
The code generated by "jaxbu" is similar to below, inside a try block:
javax.xml.bind.JAXBContext jaxbCtx = javax.xml.bind.JAXBContext.newInstance(c.getClass().getPackage().getName());
javax.xml.bind.Unmarshaller unmarshaller = jaxbCtx.createUnmarshaller();
c = (CustomerInfo) unmarshaller.unmarshal(new java.io.File("File path"));
Any help would be very much appreciated, and if you required more info. please don't hesitate to ask.
Thanks, Mathew
I am not going to be the best help with the final answer but I asked similar question a while ago and was pointed in useful directions in that question. like Blaise Doughan said you need to use something to move through the xml document and find the pieces you need then marshall them from there.
JAXB unmarshaling Ignoring the SOAP Envelope/Header tags
you shouldn't be doing the jaxb stuff directly. instead, you put all the complex types in the schema associated with your wsdl (assuming you are working from the wsdl). then you use the various jaxws tools (wsgen, wsimport) to generate the stubs and jaxb classes from the wsdl (for both the client and the server).
pretty much all of this is described in the jaxws tutorials (metro is the reference implementation of jaxws).
精彩评论