简体   繁体   English

在jax-ws webservice中泛型类会发生什么?

[英]What happens to generic class in jax-ws webservice?

I want to know if I put a generics method in jax-ws, like: 我想知道我是否在jax-ws中使用了泛型方法,例如:

public List<MyCustomClass> getSomething()

Does the jax-ws support this? jax-ws支持这个吗? On the client side what will the return of the method looks like? 在客户端,该方法的返回结果如何?

You will get a List in the client side (or an array of MyCustomClass objects if the WS consumer is written in another language). 您将在客户端获取List(如果WS使用者使用其他语言编写,则获取MyCustomClass对象的数组)。 That won't be a problem. 这不会是个问题。 Remember to always program to interfaces. 记住要始终编程到接口。

Looks like you still don't have much practice creating WS in Java, so I'll give you some advices: 看起来你仍然没有太多练习用Java创建WS,所以我会给你一些建议:

  • You must not send 2 or more objects that contains a circular reference, or you will end with circular reference problems. 不得发送包含循环引用的2个或更多对象,否则将以循环引用问题结束。 This is because the JAX-WS tool will create an interminable XML response for the request. 这是因为JAX-WS工具将为请求创建一个无限的XML响应。 It could be very hard to discover. 可能很难发现。 Let's see a case: 我们来看一个案例:

     public class ClassA { ClassB instanceOfB; //getters and setters... } public class ClassB { ClassA instanceOfA; //getters and setters... } public class MyJAXWS { @WebMethod public ClassA getClassA() { ClassA classA = new ClassA(); ClassB classB = new ClassB(); classB.setInstanceOfA(classA); classA.setInstanceOfB(classB); return classA; //boom! circular reference problems! } } 
  • You must always have interfaces in your return classes, not specific Java library classes. 必须始终在返回类中具有接口,而不是特定的Java库类。 This means, your classes should have List , Set and Map (in case of containers), because this interfaces are in higher level than the implementation classes, and you could get problems if a non-Java client tries to consume your web service method. 这意味着,您的类应该具有ListSetMap (在容器的情况下),因为此接口比实现类更高级别,并且如果非Java客户端尝试使用您的Web服务方法,您可能会遇到问题。

     public class ClassC { List<ClassA> lstClassA; //good! ArrayList<ClassB> alstClassB; //not very flexible with other languages =\\ } 
  • The classes that will go through your web services should be POJOs (Plain Old Java Objects), not service or business logic layer classes. 将通过Web服务的类应该是POJO(Plain Old Java Objects),而不是服务或业务逻辑层类。 Why? 为什么? Because only the attributes values will be marshalled/unmarshalled when communicating with the clients, no method code will appear in the contract of your Web Service. 因为在与客户端通信时,只有属性值将被编组/解组, 所以 Web服务的合同中不会出现任何方法代码

     public class ClassD { private int intValue; //naive business logic method //won't be generated in the WSDL for the clients/consumers of the Web Services public void printIntValue() { //pretty simple implementation System.out.println(this.intValue); } } 

I've faced these three problems in my last SOA project with Java. 我在上一次使用Java的SOA项目中遇到了这三个问题。 I hope other people could enhance this answer or provide info with links. 我希望其他人可以增强这个答案或提供链接信息。

Yes, that should not be a problem but usages of array is recommended. 是的,这应该不是问题,但建议使用数组。 As Luiggi mentioned you would receive a List<MyCustomClass> . 正如Luiggi所说,你会收到一个List<MyCustomClass> To add more to can find complete listing of supported types by JAX-WS here 要添加更多内容,可以在此处找到JAX-WS支持的类型的完整列表

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM