I'm using NetBeans's Web Service generation tools. 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.
So say I want a class that has 3 fields: a String, an int and a 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. I'd prefer to write standard Java classes. 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.
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.
JAX-WS uses JAXB for mapping types, so classes should conform to that specification. You can find JAXB annotations in the java.xml.bind.annotations package.
If you want to marshal a non-annotated class, conform to the rules for JavaBeans should work:
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.
If you are using NetBeans interface to design your ws.
- Click on add new operation
- Select return type, browse for your class (as shown)