Question

I'm using Netbeans to create a web application project. The IDE adds METRO2.0 libraries when I create a new web service (code first WS). The SOAP web service is well deployed in my apache Tomcat 6. However, when I send a complex type, the client couldn't access to the methods of the sended object in the client. Say I have a class called Person and an operation:

@WebMethod(operationName = "getOnePerson")
    public Person getOnePerson() {

        return new Person("MyName", "MySurname", 24);
    }

And the Person class:

public class Person {

    private String name, surname;
    private int age;

    public Person() {

    }
    public Person(String name, String surname, int age) {
        this.name = name;
        this.surname = surname;
        this.age = age;
    }

    public int getAge() {
        return age;
    }

    public String getName() {
        return name;
    }

    public String getSurname() {
        return surname;
    }
}

So how may I make the client know the Person methods? Thanks EDIT: I've tried to update my XSD file by adding Pesron's attributes:

<xs:complexType name="person">
        <xs:attribute name="name" type="xs:string" />
        <xs:attribute name="surname" type="xs:string" />
        <xs:attribute name="age" type="xs:int" />
    </xs:complexType>

By adding this portion, the client knows the getter & setter of the class Person but when trying to run the program, all the getters return null (client side):

public class Main {

    public static void main(String[] args) {

        try {
            Person p = getOnePerson();
            System.out.println(p);
            System.out.println(p.getSurname());
            System.out.println(p.getName());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static Person getOnePerson() {
        com.company.ws.BeanWebService_Service service = new com.company.ws.BeanWebService_Service();
        com.company.ws.BeanWebService port = service.getBeanWebServicePort();
        return port.getOnePerson();
    }
}

returns: com.company.ws.Person@d75415 null null So could you please tell me why the client doesn't know the different values of class's attribute?

Was it helpful?

Solution 2

What I have found and resolves the problem:

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType (name = "Person", propOrder = {"name","surname", "age"})
public class Person {
    @XmlElement(name="name",required = true)
    private String name;

    @XmlElement(name="surname",required = true)
    private String surname;

    @XmlElement(name="age",required = true)
    private int age;

//GETTER & SETTER
}

Thank you!

OTHER TIPS

As JAX-WS uses JAXB to marshal/ unmarshal objects, I recommend:

  • define a XSD for Person
  • generate the JAXB classes of this XSD
  • use the generated Person class as the return type of your Web Service

If you're using Maven (highly recommended), then you can use the following plugin to generate the JAXB classes from a XSD:

        <plugin>
            <groupId>org.jvnet.jaxb2.maven2</groupId>
            <artifactId>maven-jaxb2-plugin</artifactId>
            <version>0.8.1</version>
            <executions>
                <execution>
                    <id>generate</id>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                    <configuration>
                        <generatePackage>com.yourcompany.somecomponent.somepackage.jaxb</generatePackage>
                    </configuration>
                </execution>
            </executions>
        </plugin>

Expected XSD & xjb (JAXB customization files) location: src/main/resources

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top