jax-ws Web服务中的通用类会怎样? [英] What happens to generic class in jax-ws webservice?

查看:88
本文介绍了jax-ws Web服务中的通用类会怎样?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否在jax-ws中放入了泛型方法,例如:

  public List< MyCustomClass> getSomething()

jax-ws支持吗?
在客户端,该方法的返回将是什么样?

解决方案

您将在列表中找到一个列表。客户端(如果WS使用者使用另一种语言编写,则为MyCustomClass对象的数组)。不会有问题的。记住要始终对接口进行编程。



看起来您仍然没有太多用Java创建WS的练习,所以我会给您一些建议:




  • 一定不能发送两个或多个包含循环引用的对象,否则您将遇到循环引用问题。这是因为JAX-WS工具将为请求创建一个无休止的XML响应。可能很难发现。让我们看一个例子:

     公共类ClassA {
    ClassB instanceOfB;
    //获取器和设置器...
    }

    公共类ClassB {
    ClassA instanceOfA;
    //获取器和设置器...
    }

    公共类MyJAXWS {

    @WebMethod
    public ClassA getClassA(){
    ClassA classA = new ClassA();
    ClassB classB = new ClassB();
    classB.setInstanceOfA(classA);
    classA.setInstanceOfB(classB);
    返回classA; //繁荣!循环参考问题!
    }
    }


  • 必须始终在您的返回类中具有接口,而不是特定的Java库类。这意味着您的课程应该具有 List Set Map (对于容器而言),因为此接口的级别比实现类的级别高,并且如果非Java客户端尝试使用您的Web服务方法,则可能会遇到问题。

     公共类ClassC {

    List< ClassA> lstClassA; //好!
    ArrayList< ClassB> alstClassB; //使用其他语言不太灵活= \
    }


  • 类将通过您的Web服务的应该是POJO(普通的旧Java对象),而不是服务或业务逻辑层类。为什么?由于与客户进行通信时只会整理/取消整理属性值,因此没有方法代码将不会出现在Web服务的合同中。

     公共类ClassD {
    private int intValue;
    //天真业务逻辑方法
    //不会在WSDL中为Web服务的客户/消费者生成
    public void printIntValue(){
    //漂亮简单实现
    System.out.println(this.intValue);
    }
    }





I want to know if I put a generics method in jax-ws, like:

public List<MyCustomClass> getSomething()

Does the jax-ws support this? 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). 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:

  • You must not send 2 or more objects that contains a circular reference, or you will end with circular reference problems. This is because the JAX-WS tool will create an interminable XML response for the request. 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. 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.

    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. 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.

    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. I hope other people could enhance this answer or provide info with links.

这篇关于jax-ws Web服务中的通用类会怎样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆