JAX-WS返回一个复杂的对象?

JAX-WS返回一个复杂的对象?

问题描述:

对于Java Web Services来说我还很陌生,但是我在任何地方都找不到很好的解释.

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

我在NetBeans中有2个Java Web项目.
一个作为Web服务,另一个作为该Web服务的客户端.
我还创建了自己的名为"Person"的类,该类具有您所期望的:名称,Dob等.

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.

我想拥有一个称为"ListPeople()"的Web服务方法,该方法将返回"Person"对象的数组.

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

两个项目中都需要该类吗?
我应该先序列化对象吗?
我应该使用JAXB,如果是这样,我应该从哪里开始?

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?

很抱歉n00b问题,但我很困惑.
完成此操作的正常方法是什么?

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

预先感谢

  1. 我在两个项目中都需要参加该课程吗?是的.
  2. 我应该首先序列化对象吗?不.
  3. 如果要使用JAXB,应该从哪里开始?我不会.我更喜欢javax.oxm接口,因为我不在乎JAXB,但这是个人观点.

我个人的喜好是使用 Spring Web服务 s.如果您碰巧是Spring用户,我认为这是最好的方法.如果不是这样,也许文档仍将有助于澄清.

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.

您正在体验为什么我不喜欢您的方法的原因:服务和客户端,都依赖于类和OXM代码.您必须在两个地方都做到完美同步.更改一个,则必须同时更改两个.

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.

在这种情况下,您可以来回发送XML.从XSD模式开始.让客户端和服务而不是Java对象来处理它.您的服务将可用于非Java的客户端.

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.

如果采用这种方法,则只需要担心服务器端的OXM.您接收XML请求并将其编组到您选择的Java对象中,然后将其传递到服务层(注意:NOT Web服务层)进行处理.将响应对象转换为XML响应流,而Bob是您的叔叔.让客户处理这个问题.

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.