Using a custom class as a JAX-WS return type?

I am using NetBeans Web Services Creation Tools. I looked at the available tutorials, but can't find anything about how to use a custom class as a return type. Most of the tutorials that I read are no more complicated than Hello World: they take and return simple types like Strings.

So to say, I want the class to have 3 fields: String, int and double []. So far the only way to pass your classes is to create "envelope classes", without methods, a constructor without parameters and with all declared fields. I would rather write standard Java classes. Obviously, I cannot send methods via SOAP, but I would think that there is a way to ignore methods when the Marshalling class and only Marshall fields.

Someone told me that there are annotations that make this easier, but I cannot find tutorials on how to implement them. Any guidance would be greatly appreciated.

+7
source share
2 answers

JAX-WS uses JAXB for mapping types, so classes must conform to this specification. You can find JAXB annotations in the java.xml.bind.annotations package.

If you want to marshal a non-annotated class, you must comply with the JavaBeans rules:

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., take a look at the spec parts about factory methods and adapters.

+4
source

If you use the NetBeans interface to develop your ws.

  • Click to add a new operation.

enter image description here

  • Choose return type, find your class (as shown)
+5
source

All Articles