简体   繁体   English

使用自定义类作为JAX-WS返回类型?

[英]Using a custom class as a JAX-WS return type?

I'm using NetBeans's Web Service generation tools. 我正在使用NetBeans的Web服务生成工具。 I've looked at the tutorials available, but cannot find anything on how to use a custom class as a return type. 我查看了可用的教程,但找不到任何关于如何使用自定义类作为返回类型的内容。 Most of the tutorials I've read are no more complex than Hello World: they take and return simple types like Strings. 我读过的大多数教程都不比Hello World复杂:它们采用并返回像Strings这样的简单类型。

So say I want a class that has 3 fields: a String, an int and a double[]. 所以说我想要一个有3个字段的类:String,int和double []。 So far, the only way I can pass my own classes is by creating "envelope classes", with no methods, a parameter-less constructor, and with all fields declared public. 到目前为止,我可以传递自己的类的唯一方法是创建“包络类”,没有方法,没有参数的构造函数,并且所有字段都声明为public。 I'd prefer to write standard Java classes. 我更喜欢编写标准的Java类。 Obviously I cannot send the methods across SOAP, but I would have thought there was a way to ignore the methods when Marshalling the class, and only Marshall the fields. 显然我不能通过SOAP发送方法,但我认为有一种方法可以在编组类时忽略这些方法,并且只有Marshall这些字段。

Somebody has told me there are Annotations that facilitate this, but I can't find any tutorials on how to implement them. 有人告诉我有注释可以促进这一点,但我找不到任何关于如何实现它们的教程。 Any guidance would be greatly appreciated. 任何指导将不胜感激。

If you are using NetBeans interface to design your ws. 如果您使用NetBeans界面来设计您的ws。

  • Click on add new operation 单击添加新操作

在此输入图像描述

  • Select return type, browse for your class (as shown) 选择返回类型,浏览您的课程(如图所示)

JAX-WS uses JAXB for mapping types, so classes should conform to that specification. JAX-WS使用JAXB作为映射类型,因此类应符合该规范。 You can find JAXB annotations in the java.xml.bind.annotations package. 您可以在java.xml.bind.annotations包中找到JAXB注释。

If you want to marshal a non-annotated class, conform to the rules for JavaBeans should work: 如果要编组非注释类,请遵循JavaBeans应该工作的规则:

public class Foo {
  private String bar;
  public String getBar() { return bar; }
  public void setBar(String bar) { this.bar = bar; }

  public static void main(String[] args) {
    Foo foo = new Foo();
    foo.setBar("Hello, World!");
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    JAXB.marshal(foo, out);
    foo = (Foo)
        JAXB.unmarshal(new ByteArrayInputStream(out.toByteArray()), Foo.class);
    System.out.println(foo.getBar());
  }
}

If you want to use constructors with arguments, etc. look at the parts of the spec about factory methods and adapters. 如果要使用带参数的构造函数等,请查看有关工厂方法和适配器的规范部分。

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

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