views:

986

answers:

1

Hi, Im pretty new to Java Web Services, but I cant find a good explanation anywhere.

I have 2 Java web projects within NetBeans.
One as a web service and one as a client for that web service.
I have also created my own class called "Person", which has what you'd expect: name, dob, etc.

I would like to have a web service method called "ListPeople()" that would return an array of "Person" objects.

Do I need to have that class in both projects?
Should I be serializing the object first?
Should I be using JAXB, if so, where do I start?

Sorry for the n00b questions, but im confused.
What is the normal way of accomplishing this?

Thanks in advance

+1  A: 
  1. Do I need to have that class in both projects? Yes.
  2. Should I be serializing the object first? No.
  3. Should I be using JAXB, if so, where do I start? I would not. I prefer the javax.oxm interfaces, because I don't care for JAXB, but that's a personal opinion.

My personal preference is to use Spring web services. If you happen to be a Spring user, I think that's the best way to go. If not, perhaps the docs will still help to clarify.

You're experiencing the reason why I don't like your approach: both the service and client and dependent on the class and OXM code. You have to have it in both places, in perfect synchronization. Change one and you have to change both.

I try to minimize dependencies if I can.

And in this case you can if you send XML back and forth. Start with an XSD schema. Let the client and service deal with that instead of Java objects. Your service will be usable for clients that aren't Java that way.

If you take this approach, you only have to worry about OXM on the server side. You take in the XML request and marshal it into the Java object of your choice and pass it to your service layer (note: NOT web service layer) for processing. Turn the response object into XML response stream and Bob's your uncle. Let the client deal with that.

duffymo
Ok, so I added my class to both projects, but I get incompatible type errors.Trying to assign a service.Person (from the WS port) to a client.PersonCould you please outline steps I should take considering I only have a webservice, a webclient and a class of which that I would like to transfer objects between the server and the client.Sorry, but I still dont understand.
occhiso
See above. JAXB is just one way to marshal and unmarshal objects to XML and back.
duffymo
Hmm, what you're saying makes sense. Keeping a Java class in 2 places is not very nice at all. I will have a read up on this Spring stuff, but I think I may have a requirement to use JAX-WS. Thanks for the info.
occhiso
Too many poorly written "standards". Tell the people who imposed the requirement that it'll reduce the usability and maintainability of your service.
duffymo
As someone with the same question, thanks for your post. I still don't get one thing though - how does your XML-encoded data get passed into and out of your web service? Do I just declare an operation that takes and returns a String, and parse/construct my XML from/to those?
Mac
Since I use Spring, I can leverage their Spring web service framework classes to handle all that. The OXM interface implementations handle the marshalling and unmarshalling between XML and objects. The service interface (note: NOT web service) should have parameter and return types that match your Java requirements. The String input and output are handled by the OXM layer.
duffymo